Skip to content
Myr

Merge pull request #3260 from LBWright/error-handling

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

adds api error handling example to documentation

authored by David Lord · committed by GitHub · parents 8a7d080, 4b519f0 · Browse files · 1 file+26 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+26 added−0 removed

  • other:+26 added−0 removed

1 file+26 added−0 removed

modified:docs/patterns/errorpages.rst+26 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 %}100101102Returning API errors as JSON103----------------------------104105When using Flask for web APIs, you can use the same techniques as above106to return JSON responses to API errors. :func:`~flask.abort` is called107with a ``description`` parameter. The :meth:`~flask.errorhandler` will108use that as the JSON error message, and set the status code to 404.109110.. code-block:: python111112 from flask import abort, jsonify113114 @app.errorhandler(404)115 def resource_not_found(e):116 return jsonify(error=str(e)), 404117118 @app.route("/cheese")119 def get_one_cheese():120 resource = get_resource()121122 if resource is None:123 abort(404, description="Resource not found")124125 return jsonify(resource)