I am designing a RESTful API using Python and Flask. As expected, the API needs to receive an API request and return data if all goes well, but in the instance of an error, it needs to fail softly and return the proper error. I typically raise exceptions when an error results, but in this case I need to return the error message to the user (try-catch block?).
The way I am currently dealing with errors is to have my functions return both the data and an error, and to check for the data at each level, finally returning either the data or the error to the caller of the API function.
The problem with this is that it can get cumbersome when there are several levels of function calls, requiring my functions to pass data and errors several times and perform checks each time.
Is there a better way to do this? What are some improvements I could make to make the error propagation more simple and elegant?
Here is an example of the way I'm currently returning errors:
def get_data() data1, error = get_some_data() # function not shown if data1 is None: return None, "could not retrieve data1" data2, error = get_some_other_data() # function not shown if data2 is None: return None, "could not retrieve data2" return (data1, data2), None @app.route("/api/method", methods=['GET']) def method(): data, error = get_data() if data is None: if error is None: error = "unknown error" return json.dumps({ "error": error }), 500 return json.dumps({ "data": data }), 200
Flask-RESTful will call the handle_error() function on any 400 or 500 error that happens on a Flask-RESTful route, and leave other routes alone. You may want your app to return an error message with the correct media type on 404 Not Found errors; in which case, use the catch_all_404s parameter of the Api constructor.
Flask gives you the ability to raise any HTTP exception registered by Werkzeug. However, the default HTTP exceptions return simple exception pages. You might want to show custom error pages to the user when an error occurs. This can be done by registering error handlers.
You could use abort(http_code)
to return an appropriate http code to the client or just raise a non-http exception. And use @app.errorhandler()
decorator to provide a custom handler for http errors and arbitrary exceptions. You could also use an ordinary try/except block where you are ready to handle an exception. Python is not Go you can use exceptions.
Maybe the Flask API is more suitable for your needs, as it is particularly designed for RESTful APIs.
It has better exception handling than Flask, see: http://www.flaskapi.org/api-guide/exceptions/
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