Coder Social home page Coder Social logo

aiohttp_exc_handlers's Introduction

Bind views to exceptions for aiohttp

https://travis-ci.org/zzzsochi/aiohttp_exc_handlers.svg?branch=master

Usage

import asyncio

from aiohttp import web
from aiohttp_exc_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

aiohttp_exc_handlers's People

Contributors

zzzsochi avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

demmsnt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.