pmatos
pmatos

Reputation: 296

How to handle twilio exceptions by error code and process them

I am using the following approach for handling Twilio exceptions in Python:

try:
    #code for sending the sms
    print(message.sid)
except TwilioRestException as e:
    print(e)

This allows me to send sms and Exceptions are handled by Python.

Now I need to "return" the exceptions codes in order to process them, let's say, give user a message depending on the exception.

How can I achieve that?

Upvotes: 0

Views: 2544

Answers (1)

mrEvgenX
mrEvgenX

Reputation: 766

If raising exception is not an option you could simply add return under except block.

def func():
    # your logic
    try:
        #code for sending the sms
        print(message.sid)
    except TwilioRestException as e:
        print(e)
        return e.code  # or whetever attribute e has for it...

By default function will return None if everything goes right. In client code it will be like

err = func()
if err:
    # sms wasn't sent, action required

Upvotes: 3

Related Questions