From 24673e440804d313b205bfdd6661992554cf6983 Mon Sep 17 00:00:00 2001 From: Maxime Alves LIRMM Date: Wed, 8 Jul 2020 12:45:01 +0200 Subject: [PATCH] [exceptions] add exception handlers, moved AclCallerMiddleware to lib --- halfapi/app.py | 13 +++++++++--- halfapi/lib/responses.py | 45 ++++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/halfapi/app.py b/halfapi/app.py index 040ea35..72ac998 100644 --- a/halfapi/app.py +++ b/halfapi/app.py @@ -20,10 +20,11 @@ RequestResponseEndpoint = Callable[ [Request], Awaitable[Response] ] from .models.api.domain import Domain # module libraries -from .lib.responses import * from .lib.jwt_middleware import JWTAuthenticationBackend from .lib.acl_caller_middleware import AclCallerMiddleware +from .lib.responses import * + def mount_domains(app: ASGIApp, domains: list): """ Procedure to mount the registered domains on their prefixes @@ -89,7 +90,7 @@ async def root(request): def check_conf(): if not environ.get('HALFORM_SECRET', False): - environ['HALFORM_SECRET'] = 'secret' + environ['HALFORM_SECRET'] = open('/etc/half_orm/secret').read() print('Missing HALFORM_SECRET variable from configuration, seting to default') if not environ.get('HALFORM_DSN', False): @@ -100,5 +101,11 @@ app = Starlette( Middleware(AuthenticationMiddleware, backend=JWTAuthenticationBackend(secret_key=environ.get('HALFORM_SECRET'))), Middleware(AclCallerMiddleware), ], - on_startup=[startup] + exception_handlers={ + 401: UnauthorizedResponse, + 404: NotFoundResponse, + 500: InternalServerErrorResponse, + 501: NotImplementedResponse + }, + on_startup=[startup], ) diff --git a/halfapi/lib/responses.py b/halfapi/lib/responses.py index 0f298fa..d4a5c3c 100644 --- a/halfapi/lib/responses.py +++ b/halfapi/lib/responses.py @@ -7,17 +7,11 @@ from io import TextIOBase, StringIO # asgi framework from starlette.responses import Response -class NotFoundResponse(Response): - """ The 404 Not Found default Response - """ - def __init__(self): - super().__init__(status_code=404) - -class ForbiddenResponse(Response): - """ The 401 Not Found default Response - """ - def __init__(self): - super().__init__(status_code = 401) +__all__ = ['CSVResponse', + 'InternalServerErrorResponse', + 'NotFoundResponse', + 'NotImplementedResponse', + 'UnauthorizedResponse'] class CSVResponse(Response): def __init__(self, obj): @@ -33,3 +27,32 @@ class CSVResponse(Response): 'Content-Type': 'text/csv; charset=UTF-8', 'Content-Disposition': f'attachment; filename="{filename}"'}, status_code = 200) + + +class InternalServerErrorResponse(Response): + """ The 500 Internal Server Error default Response + """ + def __init__(self): + super().__init__(status_code=500) + + +class NotFoundResponse(Response): + """ The 404 Not Found default Response + """ + def __init__(self): + super().__init__(status_code=404) + + +class NotImplementedResponse(Response): + """ The 501 Not Implemented default Response + """ + def __init__(self): + super().__init__(status_code=501) + + +class UnauthorizedResponse(Response): + """ The 401 Not Found default Response + """ + def __init__(self): + super().__init__(status_code = 401) +