Robert Sinclair
Robert Sinclair

Reputation: 5416

Twilio catch exception while creating client object

I'm trying to check that Twilio object was properly instantiated, but it doesn't work

I do

try {
    $twilioObj = new Client($sid, $token);
}
catch (Exception $e){
    echo $e->getMessage();
}

^ Regardless if the tokens are valid or not it never results in an error. I noticed that it always generated an object.. So even if I put some random token, I still get an object back.

What is the best way of catching this?

I did is as per documentation: https://www.twilio.com/docs/libraries/php/usage-guide#exceptions but it doesn't work.

Upvotes: 0

Views: 42

Answers (1)

philnash
philnash

Reputation: 73057

The Twilio Client object doesn't know at the time of instantiation whether the credentials you have provided it are valid or not, so it cannot throw an error at that point.

If the credentials are not valid, the client will throw an error when you use it to make a request to the API. You should catch the error there.

As per the documentation, instantiating a Client without a username or password will throw a ConfigurationException.

Upvotes: 1

Related Questions