0

I am working with Twilio's API to return information about phone numbers. Some of the phone numbers are invalid and return an error such as

Traceback (most recent call last):
  File "test_twilio.py", line 17, in <module>
    number = client.lookups.phone_numbers("(4154) 693-
6078").fetch(type="carrier")
  File "/Users/jawnsano/anaconda/lib/python2.7/site-
packages/twilio/rest/lookups/v1/phone_number.py", line 158, in fetch
    params=params,
  File "/Users/jawnsano/anaconda/lib/python2.7/site-
packages/twilio/base/version.py", line 82, in fetch
    raise self.exception(method, uri, response, 'Unable to fetch 
record')
twilio.base.exceptions.TwilioRestException: 
HTTP Error Your request was:

GET /PhoneNumbers/(4154) 693-6078

Twilio returned the following information:

Unable to fetch record: The requested resource /PhoneNumbers/(4154) 
693-6078 was not found

More information may be available here:

https://www.twilio.com/docs/errors/20404

If an error like the one shown above is returned, I want to print 'There is an error.' However, for my if statement, is there a way to make Python print that for when there is a traceback error/error in general? I think there is probably a better way than setting making it like

if returned_value = (super long error message):
    etc...
2
  • 1
    can you share your code for better understanding.? Commented Sep 7, 2017 at 7:15
  • The error message is not returned. Commented Sep 7, 2017 at 7:20

2 Answers 2

1

You use try and except to catch errors.

from twilio.base.exceptions import TwilioRestException

try:
    ... your code
except TwilioRestException:
    print("whatever")
Sign up to request clarification or add additional context in comments.

1 Comment

Note to future developers: you'll need to import TwilioRestException from twilio.base.exceptions.
0

For this specific exception:

try:
    the_function_that_raises_the_exception()
except twilio.base.exceptions.TwilioRestException as e:
    print("Oops, exception encountered:\n" + str(e))

Note that you probably need to call import twilio.base.exceptions first.

For any exception:

try:
    the_function_that_raises_the_exception()
except Exception as e:
    print(e)

Be careful when using the second approach though - this catches ALL exceptions, and might mask a bigger problem if not addressed properly. Unless you know where the exception originates from (but if that is the case, you know the type and you can filter only that type), sometimes this approach can be used:

try:
    the_function_that_can_raise_numerous_exceptions()
except Exception as e:
    with open("exceptions.txt", "a") as f:
        f.write(e)
    # or even send an email here
    raise

This makes sure that the exception is caught (by except), then written to a file and then reraised. This will still cause the script to fail, but will have a log to look into.

2 Comments

You shouldn't recommend catching all exceptions. That's almost never the right thing to do.
Yup, was planning on extending the answer to warn about this approach - done that just now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.