Skip to content

Instantly share code, notes, and snippets.

@kgriffs
Created July 28, 2022 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgriffs/9329e859e2c8e103ea2c0efc4c75c3ee to your computer and use it in GitHub Desktop.
Save kgriffs/9329e859e2c8e103ea2c0efc4c75c3ee to your computer and use it in GitHub Desktop.
Falcon recipe: Log an error whenever an error status code is returned
import logging
import falcon
import falcon.asgi
import falcon.util
class HelloResource:
async def on_get(self):
pass
class AuthMiddleware:
async def process_request(self, req, resp):
raise falcon.HTTPForbidden()
async def process_response(self, req, resp, resource, req_succeeded):
status_code = falcon.util.http_status_to_code(resp.status)
if status_code >= 400:
# NOTE: Could also report to an issue tracker such as Sentry.
logging.error(f'Responding with an error status: {status_code}')
def create_app():
app = falcon.asgi.App()
app.add_middleware(AuthMiddleware())
app.add_route('/', HelloResource())
return app
app = create_app()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment