Skip to content

Instantly share code, notes, and snippets.

@yuvalherziger
Last active January 6, 2022 19:18
Show Gist options
  • Save yuvalherziger/86d32e1cd42911b45b1d6a0e2da3d2fd to your computer and use it in GitHub Desktop.
Save yuvalherziger/86d32e1cd42911b45b1d6a0e2da3d2fd to your computer and use it in GitHub Desktop.
app.py
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