twilio catching error does not work

2019-05-21 08:16发布

I am implementing twilio in my laravel 5 application. To use it in the framework I use aloha/laravel-twilio integration.

Sending a valid request with test-credentials works fine. I have problems when I want to implement an error-handling.

For some reason the catch does not get the error, which results in a crash of the app. The error seems to be in the twilio-sdk if I read the error message correctly.

Here is what I've done so far:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Aloha\Twilio\TwilioInterface;

class Activation extends Model {
    protected $fillable = array( 'a', 'b', 'c');
    public static function send() {

        // Testaccount
        // $toNumber = '+15005550006'; // valid number; works fine
        $toNumber = '+15005550001'; // @todo will throw an exeption, and breaks the app
        try {
            \Twilio::message( $toNumber, 'Pink Elephants and Happy Rainbows');
        } catch ( Services_Twilio_RestException $e ) {
            elog( 'EACT', $e->getMessage(  ) , __FUNCTION__ );  // this is not called when an twilio error occurs
        }
    }
}

This results in the following error:

Whoops, looks like something went wrong.
Services_Twilio_RestException in /path/to/my/laravel/vendor/twilio/sdk/Services/Twilio.php line 297 
Exception_message: The 'To' number +15005550001 is not a valid phone number.

From the documentation this error (not valid phone numer) shall be thrown, but I should have a possiblity to catch and process it. Currently, this does not work. I do not get the error catched...

How can I get the twilio-errors catched and processed?

3条回答
男人必须洒脱
2楼-- · 2019-05-21 08:46

Today (19-May-2017) the code is like this :

    // Step 1: set our AccountSid and AuthToken from https://twilio.com/console
    $AccountSid = "XXX";
    $AuthToken = "XXX";

    $client = new Client($AccountSid, $AuthToken);           

    try {
        $sms = $client->account->messages->create(

            // the number we are sending to - Any phone number
            $number,

            array(
               // Step 2: Change the 'From' number below to be a valid Twilio number
                // that you've purchased
                'from' => "+XXXXXXXXXXX",

                // the sms body
                'body' => $sms
            )
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name";

    } catch (TwilioException $e) {
        die( $e->getCode() . ' : ' . $e->getMessage() );
    }
查看更多
地球回转人心会变
3楼-- · 2019-05-21 09:03

The class is in a namespace, so I have to reference the absolut class exception - \Services_Twilio_RestException - in the catch .

It works with this code:

    try {
        \Twilio::message( $toNumber, 'Pink Elephants and Happy Rainbows');
    } catch ( \Services_Twilio_RestException $e ) {
        elog( 'EACT', $e->getMessage(  ) , __FUNCTION__ );  
    }
查看更多
我只想做你的唯一
4楼-- · 2019-05-21 09:09

See below which is valid as of today. TwilioException is not valid and neither is Services_Twilio_RestException. You should use Exception instead.

My use case is I had to send to a database of numbers and not have an invalid phone number break my script. We did some work-around a month or two ago which involved logging when a message was sent and had a cron job checking where we left off every two minutes... not efficient when you're sending tens of thousands of text messages.

require_once '../Twilio/autoload.php'; // Loads the library

use Twilio\Rest\Client;

//some test fail numbers
$arr = array(1234567890,"11855976lend1",321619819815,198198195616516);


/* ==================================================================================
//create a function to send SMS using copilot (uses an SID instead of a phone number)
   ================================================================================*/
function sendSMS($to){
  // Download the PHP helper library from twilio.com/docs/php/install
  // These vars are your accountSid and authToken from twilio.com/user/account
  $account_sid = 'xxx';
  $auth_token = 'xxx';
  $client = new Client($account_sid, $auth_token);

  //this nifty little try/catch will save us pain when we encounter bad phone numbers
  try{
    $client->messages->create(
      $to,
      array(
          'messagingServiceSid' => "MGxxx",
          'body' => "This is the body we're sending."
      )
    );

    //sent successfully
    echo "sent to $to successfully<br>";
  }catch(Exception $e){
    echo $e->getCode() . ' : ' . $e->getMessage()."<br>";
  }

}


foreach($arr as &$value){
  sendSMS($value);
}

//remember to unset the pointer so you don't run into issues if re-using
unset($value);
查看更多
登录 后发表回答