Last active
January 6, 2022 19:18
-
-
Save yuvalherziger/86d32e1cd42911b45b1d6a0e2da3d2fd to your computer and use it in GitHub Desktop.
app.py
This file contains 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
from aiohttp import web | |
from aiohttp_catcher import catch, Catcher | |
async def divide(request): | |
quotient = int(request.query.get("x")) / int(request.query.get("y")) | |
return web.json_response(data={"quotient": quotient}) | |
msg = "Zero division makes zero sense" | |
async def main(): | |
catcher = Catcher() | |
await catcher.add_scenario( | |
catch(ZeroDivisionError).with_status_code(400).and_return(msg) | |
) | |
# Register your catcher as an aiohttp middleware: | |
app = web.Application(middlewares=[catcher.middleware]) | |
app.add_routes([web.get("/divide", divide)]) | |
web.run_app(app) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment