Created
July 28, 2022 18:36
-
-
Save kgriffs/9329e859e2c8e103ea2c0efc4c75c3ee to your computer and use it in GitHub Desktop.
Falcon recipe: Log an error whenever an error status code is returned
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 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