Skip to content
Myr

adds api error handling documentation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Logan Wright · committed by David Lord · parent 8a7d080 · Browse files · 1 file+27 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+27 added−0 removed

  • other:+27 added−0 removed

1 file+27 added−0 removed

modified:docs/patterns/errorpages.rst+27 added
95 unmodified lines
96 <h1>Page Not Found</h1>97 <p>What you were looking for is just not there.98 <p><a href="{{ url_for('index') }}">go somewhere nice</a>99 {% endblock %}100101102Handling API Errors with Abort103------------------------------104105When using Flask for web APIs, handling errors is as simple as the previous examples but with minor changes.106107Register the error handler::108109 from flask import jsonify110111 @app.errorhandler(404)112 def resource_not_found(e):113 # if passing in an Exception object directly, you may need to convert it to a string114 return jsonify(error=str(e)), 404115116To use this error handler::117118 @app.route('/cheese', methods=['GET'])119 def get_one_cheese():120 # logic to find your resource121 if (resource is None):122 abort(404, 'Resource not found')123 else:124 return jsonify(resource=resource)125126In the above example, the error handler is invoked with the second argument passed to it. It returns the json message with the response.