Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+64 added−0 removed
other:+64 added−0 removed
Provenance
Commit
f9f8180
Tree
f328939
Author
Armin Ronacher <armin.ronacher@active-4.com>
Committer
Armin Ronacher <armin.ronacher@active-4.com>
Parent
fc2225b
2 files changed+64 added−0 removed
added:docs/patterns/apierrors.rst+63 added
1+Implementing API Exceptions2+===========================3+4+It's very common to implement RESTful APIs on top of Flask. One of the5+first thing that developers run into is the realization that the builtin6+exceptions are not expressive enough for APIs and that the content type of7+``text/html`` they are emitting is not very useful for API consumers.8+9+The better solution than using ``abort`` to signal errors for invalid API10+usage is to implement your own exception type and install an error handler11+for it that produces the errors in the format the user is expecting.12+13+Simple Exception Class14+----------------------15+16+The basic idea is to introduce a new exception that can take a proper17+human readable message, a status code for the error and some optional18+payload to give more context for the error.19+20+This is a simple example::21+22+ from flask import jsonfiy23+24+ class InvalidUsage(Exception):25+ status_code = 40026+27+ def __init__(self, message, status_code=None, payload=None):28+ Exception.__init__(self)29+ self.message = message30+ if status_code is not None:31+ self.status_code = status_code32+ self.payload = payload33+34+ def to_dict(self):35+ rv = dict(self.payload or ())36+ rv['message'] = self.message37+ return rv38+39+A view can now raise that exception with an error message. Additionally40+some extra payload can be provided as a dictionary through the `payload`41+parameter.42+43+Registering an Error Handler44+----------------------------45+46+At that point views can raise that error, but it would immediately result47+in an internal server error. The reason for this is that there is no48+handler registered for this error class. That however is easy to add::49+50+ @app.errorhandler(InvalidAPIUsage)51+ def handle_invalid_usage(error):52+ response = jsonify(error.to_dict())53+ response.status_code = error.status_code54+ return response55+56+Usage in Views57+--------------58+59+Here is how a view can use that functionality::60+61+ @app.route('/foo')62+ def get_foo():63+ raise InvalidUsage('This view is gone', status_code=410)