Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion core/api/middleware/exception_handling_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand All @@ -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)
Comment on lines +34 to +37
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new shouldSquashClientExceptions flag introduces an alternate logging path for ClientException (the else: logging.exception(exception) branch), but there are currently no tests exercising this behavior (e.g. constructing the app with ExceptionHandlingMiddleware(shouldSquashClientExceptions=False) and asserting stack traces are logged / responses unchanged). Consider adding a test case alongside the existing middleware tests (e.g. in tests/api/test_json_route.py or test_streaming_json_route.py) to cover the non-squashed path and guard against regressions in this error-handling behavior.

Copilot uses AI. Check for mistakes.
response = self._convert_exception(exception=exception)
except KibaException as exception:
logging.exception(exception)
Expand Down
3 changes: 2 additions & 1 deletion core/queues/message_queue_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading