async def test_unit__general_exception_handling__ok__nominal_case( self, aiohttp_client, loop): hapic = Hapic(async_=True, processor_class=MarshmallowProcessor) @hapic.with_api_doc() async def hello(request): return 1 / 0 app = web.Application(debug=True) app.router.add_get("/", hello) context = AiohttpContext( app, default_error_builder=MarshmallowDefaultErrorBuilder()) context.handle_exception(ZeroDivisionError, 400) hapic.set_context(context) client = await aiohttp_client(app) resp = await client.get("/") json = await resp.json() assert resp.status == 400 assert { "details": { "error_detail": {} }, "message": "division by zero", "code": None, } == json
def worldmapc_web_app(worldmapc_kernel: Kernel, loop, aiohttp_client) -> TestClient: app = get_application(worldmapc_kernel) context = AiohttpContext(app, debug=True, default_error_builder=ErrorBuilder()) context.handle_exception(HTTPNotFound, http_code=404) context.handle_exception(Exception, http_code=500) hapic.reset_context() hapic.set_processor_class(RollingSerpycoProcessor) hapic.set_context(context) return loop.run_until_complete(aiohttp_client(app))
async def test_unit__global_exception__ok__nominal_case(self, aiohttp_client): hapic = Hapic(async_=True, processor_class=MarshmallowProcessor) @hapic.with_api_doc() async def divide_by_zero(request): raise ZeroDivisionError() app = web.Application(debug=True) context = AiohttpContext(app) hapic.set_context(context) context.handle_exception(ZeroDivisionError, http_code=HTTPStatus.BAD_REQUEST) app.router.add_get("/", divide_by_zero) client = await aiohttp_client(app) response = await client.get("/") assert 400 == response.status
def run(args: argparse.Namespace) -> None: # Configure logging if args.debug: configure_logging(logging.DEBUG) else: configure_logging(logging.INFO) kernel = get_kernel( args.world_map_source, args.tile_maps_folder, args.game_config_folder, server_db_path=args.server_db_path, ) server_logger.info("Create web application") app = get_application(kernel) # Configure hapic server_logger.info("Configure web api") context = AiohttpContext(app, debug=args.debug, default_error_builder=ErrorBuilder()) context.handle_exception(HTTPNotFound, http_code=404) context.handle_exception(UserDisplayError, http_code=400) context.handle_exception(Exception, http_code=500) hapic.set_processor_class(RollingSerpycoProcessor) hapic.set_context(context) hapic.add_documentation_view("/doc") kernel.init() server_logger.info("Start server listening on {}:{}".format( args.host, args.port)) web.run_app(app, host=args.host, port=args.port, access_log=server_logger)
async def test_func__catch_one_exception__ok__aiohttp_case( self, test_client): from aiohttp import web app = web.Application() hapic = Hapic(processor_class=MarshmallowProcessor) context = AiohttpContext(app=app) hapic.set_context(context) async def my_view(request): raise ZeroDivisionError("An exception message") app.add_routes([web.get("/my-view", my_view)]) # FIXME - G.M - 17-05-2018 - Verify if: # - other view that work/raise an other exception do not go # into this code for handle this exceptions. # - response come clearly from there, not from web framework: # Check not only http code, but also body. # see issue #158 (https://github.com/algoo/hapic/issues/158) context.handle_exception(ZeroDivisionError, http_code=400) test_app = await test_client(app) response = await test_app.get("/my-view") assert 400 == response.status