Files
deb-python-falcon/docs/api/errors.rst
2017-06-30 19:42:11 -05:00

2.6 KiB

Error Handling

When it comes to error handling, you can always directly set the error status, appropriate response headers, and error body using the resp object. However, Falcon tries to make things a little easier by providing a set of error classes you can raise when something goes wrong. All of these classes inherit from ~.HTTPError.

Falcon will convert any instance or subclass of ~.HTTPError raised by a responder, hook, or middleware component into an appropriate HTTP response. The default error serializer supports both JSON and XML. If the client indicates acceptance of both JSON and XML with equal weight, JSON will be chosen. Other media types may be supported by overriding the default serializer via ~.API.set_error_serializer.

Note

If a custom media type is used and the type includes a "+json" or "+xml" suffix, the default serializer will convert the error to JSON or XML, respectively.

All classes are available directly in the falcon package namespace:

import falcon

class MessageResource(object):
    def on_get(self, req, resp):

        # ...

        raise falcon.HTTPBadRequest(
            'TTL Out of Range',
            'The message's TTL must be between 60 and 300 seconds, inclusive.'
        )

        # ...

Note also that any exception (not just instances of ~.HTTPError) can be caught, logged, and otherwise handled at the global level by registering one or more custom error handlers. See also ~.API.add_error_handler to learn more about this feature.

Base Class

falcon.HTTPError

Mixins

falcon.http_error.NoRepresentation

Predefined Errors

falcon