Last active
March 22, 2024 19:39
-
-
Save vytas7/068ac5bd8a0fd118052d32e30be67792 to your computer and use it in GitHub Desktop.
Falcon error handling examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import falcon | |
import falcon.routing | |
class FloatConverter(falcon.routing.BaseConverter): | |
def convert(self, value): | |
try: | |
return float(value) | |
except ValueError: | |
return None | |
def serialize_error(req, resp, exception): | |
preferred = req.client_prefers((falcon.MEDIA_JSON,)) | |
if preferred == falcon.MEDIA_JSON: | |
resp.content_type = preferred | |
resp.data = exception.to_json() | |
else: | |
report = ['[Custom error serializer]\n'] | |
for key, value in sorted(exception.to_dict().items()): | |
report.append(f'{key}: {value}\n') | |
resp.content_type = falcon.MEDIA_TEXT | |
resp.text = ''.join(report) | |
resp.append_header('Vary', 'Accept') | |
def handle_overflow(req, resp, ex, params): | |
if 'power' in params and params['power'] >= 1000: | |
# Reraise an instance of HTTPError | |
raise falcon.HTTPBadRequest(title='Too Damn High!') | |
# Render a custom error response | |
resp.content_type = falcon.MEDIA_TEXT | |
resp.text = 'That was too much to handle... Check your parameters.\n' | |
resp.status = falcon.HTTP_UNPROCESSABLE_ENTITY | |
class Division: | |
def on_get(self, req, resp, dividend, divisor): | |
# Divide by 0 to make Falcon return a generic 500 response | |
resp.media = dividend / divisor | |
class Exponentiation: | |
def on_get(self, req, resp, base, power): | |
resp.media = base ** power | |
app = falcon.App() | |
app.router_options.converters['float'] = FloatConverter | |
app.add_error_handler(OverflowError, handle_overflow) | |
app.set_error_serializer(serialize_error) | |
app.add_route('/division/{dividend:float}/{divisor:float}', Division()) | |
app.add_route('/exponentiation/{base:float}^{power:float}', Exponentiation()) |
does this make sense to have this in the example folder?
Probably it does, but it needs to be cleaned up a bit. I'll drop a link on the respective GH issue: falconry/falcon#1460.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Different ways to handle and format errors in Falcon.
Run as
gunicorn error_handling:app
(or similar, adapt to your WSGI server of choice).Default handler for otherwise unhandled exceptions
Format the error as plain text using a custom error serializer
Render an error response in the error handler
Raise an instance of HTTPError from the error handler
(
http
in the above snippets is the HTTPie HTTP client.)