How can I reuse exception handling code for multiple functions in Python?
I am working on a project that will use the Stripe Python library. https://stripe.com/docs/api/python#errors
This is some example code from their docs.
try:
# Use Stripe's bindings...
pass
except stripe.error.CardError, e:
# Since it's a decline, stripe.error.CardError will be caught
body = e.json_body
err = body['error']
print "Status is: %s" % e.http_status
print "Type is: %s" % err['type']
print "Code is: %s" % err['code']
# param is '' in this case
print "Param is: %s" % err['param']
print "Message is: %s" % err['message']
except stripe.error.InvalidRequestError, e:
# Invalid parameters were supplied to Stripe's API
pass
except stripe.error.AuthenticationError, e:
# Authentication with Stripe's API failed
# (maybe you changed API keys recently)
pass
except stripe.error.APIConnectionError, e:
# Network communication with Stripe failed
pass
except stripe.error.StripeError, e:
# Display a very generic error to the user, and maybe send
# yourself an email
pass
except Exception, e:
# Something else happened, completely unrelated to Stripe
pass
I need to write several functions that perform various calls into the Stripe system to process my transactions. For example; retrieve a token, create a customer, charge a card, etc. Do I have to repeat the try/except code in each function, or is there a way to make the contents of the try block dynamic?
I'd like to use these various functions in my Flask view code as conditionals so if I could get back an error/success message from each of them, that would be helpful too.
By handling multiple exceptions, a program can respond to different exceptions without terminating it. In Python, try-except blocks can be used to catch and respond to one or multiple exceptions. In cases where a process raises more than one possible exception, they can all be handled using a single except clause.
And when it comes to reusing code in Python, it all starts and ends with the humble function. Take some lines of code, give them a name, and you've got a function (which can be reused). Take a collection of functions and package them as a file, and you've got a module (which can also be reused).
Python uses try and except keywords to handle exceptions. Both keywords are followed by indented blocks. The try: block contains one or more statements which are likely to encounter an exception. If the statements in this block are executed without an exception, the subsequent except: block is skipped.
According to the Python Documentation: A try statement may have more than one except clause, to specify handlers for different exceptions. At most one handler will be executed. In this example, we have two except clauses.
Write a decorator that calls the decorated view within the try block and handles any Stripe-related exceptions.
from functools import wraps
def handle_stripe(f):
@wraps(f)
def decorated(*args, **kwargs):
try:
return f(*args, **kwargs)
except MyStripeException as e:
return my_exception_response
except OtherStripeException as e:
return other_response
return decorated
@app.route('/my_stripe_route')
@handle_stripe
def my_stripe_route():
do_stripe_stuff()
return my_response
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With