diff --git a/docs/patterns/errorpages.rst b/docs/patterns/errorpages.rst index 1df9c061..31805171 100644 --- a/docs/patterns/errorpages.rst +++ b/docs/patterns/errorpages.rst @@ -97,3 +97,30 @@ An example template might be this:
What you were looking for is just not there.
go somewhere nice {% endblock %} + + +Handling API Errors with Abort +------------------------------ + +When using Flask for web APIs, handling errors is as simple as the previous examples but with minor changes. + +Register the error handler:: + + from flask import jsonify + + @app.errorhandler(404) + def resource_not_found(e): + # if passing in an Exception object directly, you may need to convert it to a string + return jsonify(error=str(e)), 404 + +To use this error handler:: + + @app.route('/cheese', methods=['GET']) + def get_one_cheese(): + # logic to find your resource + if (resource is None): + abort(404, 'Resource not found') + else: + return jsonify(resource=resource) + +In the above example, the error handler is invoked with the second argument passed to it. It returns the json message with the response. \ No newline at end of file