Skip to main content

Bind views to exceptions for aiohttp

Project description

https://travis-ci.org/zzzsochi/aiohttp_exc_handlers.svg?branch=master https://coveralls.io/repos/zzzsochi/aiohttp_exc_handlers/badge.svg

Usage

import asyncio

from aiohttp import web
from aiohttp_ext_handlers import (
    exc_handlers_middleware,
    bind_exc_handler,
)


class CustomException(Exception):
    pass


async 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

$ pip install pytest
$ py.test tests.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttp_exc_handlers-0.1.tar.bz2 (2.2 kB view details)

Uploaded Source

File details

Details for the file aiohttp_exc_handlers-0.1.tar.bz2.

File metadata

File hashes

Hashes for aiohttp_exc_handlers-0.1.tar.bz2
Algorithm Hash digest
SHA256 1c8c314042c4fd6fea5cf6dd328ff85ae07004afdfcb82ecfd822498327dfbb6
MD5 908934829d1df8de1dd3db6742e801cf
BLAKE2b-256 5f5c9351677497f38b82874e24f65b2ab492ff4f54ec70070c81b727e5448772

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page