How can I reuse exception handling code for multiple functions in Python?

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 an example code from their docs.

try: # Use Stripe bindings... pass except stripe.error.CardError, e: # Since it 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 API pass except stripe.error.AuthenticationError, e: # Authentication with Stripe 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 some functions that make various calls on the Stripe system to process my transactions. For instance; get a token, create a client, charge a card, etc. Should I repeat the try / except code in each function or is there a way to make the contents of the test block dynamic?

I would like to use these various functions in my Flask view code as conditional, so if I could get an error / success message from each of them, that would be useful too.

+6
source share
1 answer

Write a decorator that calls the decorated look in 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 
+7
source

Source: https://habr.com/ru/post/983597/


All Articles