https://github.com/zzzsochi/aiohttp_exc_handlers
Bind views to exceptions for aiohttp
https://github.com/zzzsochi/aiohttp_exc_handlers
aiohttp aiohttp-server asyncio exception-handler exception-handling
Last synced: 3 months ago
JSON representation
Bind views to exceptions for aiohttp
- Host: GitHub
- URL: https://github.com/zzzsochi/aiohttp_exc_handlers
- Owner: zzzsochi
- Created: 2015-11-07T23:17:42.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-01-19T17:53:12.000Z (over 9 years ago)
- Last Synced: 2025-02-03T14:51:53.489Z (5 months ago)
- Topics: aiohttp, aiohttp-server, asyncio, exception-handler, exception-handling
- Language: Python
- Size: 4.88 KB
- Stars: 1
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
Awesome Lists containing this project
README
====================================
Bind views to exceptions for aiohttp
====================================.. image:: https://travis-ci.org/zzzsochi/aiohttp_exc_handlers.svg?branch=master
:target: https://travis-ci.org/zzzsochi/aiohttp_exc_handlers
:align: right.. image:: https://coveralls.io/repos/zzzsochi/aiohttp_exc_handlers/badge.svg
:target: https://coveralls.io/github/zzzsochi/aiohttp_exc_handlers
:align: right-----
Usage
-----.. code:: python
import asyncio
from aiohttp import web
from aiohttp_exc_handlers import (
exc_handlers_middleware,
bind_exc_handler,
)class CustomException(Exception):
passasync def custom_exception_handler(request, exc):
return web.Response(text="Hello, {!s}!".format(exc))async def hello(request):
raise CustomException('world')# add middleware
app = web.Application(middlewares=[exc_handlers_middleware])# bind handler to exception
bind_exc_handler(app, CustomException, custom_exception_handler)app.router.add_route('GET', '/', hello)
loop = asyncio.get_event_loop()
handler = app.make_handler()
f = loop.create_server(handler, '0.0.0.0', 8080)
srv = loop.run_until_complete(f)try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
loop.run_until_complete(handler.finish_connections(1.0))
srv.close()
loop.run_until_complete(srv.wait_closed())
loop.run_until_complete(app.finish())loop.close()
-----
Tests
-----.. code:: shell
$ pip install pytest
$ py.test tests.py