diff --git a/core/api/middleware/exception_handling_middleware.py b/core/api/middleware/exception_handling_middleware.py index 6deba92..85bb769 100644 --- a/core/api/middleware/exception_handling_middleware.py +++ b/core/api/middleware/exception_handling_middleware.py @@ -3,6 +3,7 @@ from starlette.requests import Request from starlette.responses import JSONResponse from starlette.responses import Response +from starlette.types import ASGIApp from core import logging from core.exceptions import ClientException @@ -11,6 +12,10 @@ class ExceptionHandlingMiddleware(BaseHTTPMiddleware): + def __init__(self, app: ASGIApp, shouldSquashClientExceptions: bool = True) -> None: + super().__init__(app=app) + self.shouldSquashClientExceptions = shouldSquashClientExceptions + @staticmethod def _convert_exception(exception: KibaException) -> Response: response = JSONResponse(status_code=exception.statusCode, content=exception.to_dict()) @@ -26,7 +31,10 @@ async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) - if not hasattr(exception, 'shouldAddCacheHeader') or exception.shouldAddCacheHeader: response.headers['Cache-Control'] = f'max-age={60 * 60 * 24 * 365}' except ClientException as exception: - logging.error(f'{exception.exceptionType} occurred: {exception.message}') + if self.shouldSquashClientExceptions: + logging.error(f'{exception.exceptionType} occurred: {exception.message}') + else: + logging.exception(exception) response = self._convert_exception(exception=exception) except KibaException as exception: logging.exception(exception) diff --git a/core/queues/message_queue_processor.py b/core/queues/message_queue_processor.py index 4e91cb1..caad38d 100644 --- a/core/queues/message_queue_processor.py +++ b/core/queues/message_queue_processor.py @@ -53,8 +53,9 @@ async def _process_message(self, message: MessageType) -> None: statusCode = exception.statusCode if isinstance(exception, KibaException) else 500 logging.error('Caught exception whilst processing message:') logging.exception(exception) + kibaException = KibaException.from_exception(exception=exception) for client in self.notificationClients: - await client.post(messageText=f'Error processing message: {message.command}\n```\n{requestId}\n{message.content}\n{exception}```') + await client.post(messageText=f'Error processing message: {message.command}\n```\n{requestId}\n{message.content}\n{kibaException.message}```') # TODO(krishan711): should possibly reset the visibility timeout duration = time.time() - startTime logging.api(action='MESSAGE', path=message.command, pathPattern=message.command, query=query, response=statusCode, duration=duration)