decorator: Stop stringifying exceptions unnecessarily.

This commit is contained in:
Alex Vandiver 2023-10-11 18:48:00 +00:00 committed by Tim Abbott
parent 42b6b03ee7
commit 8a6f96acad
2 changed files with 9 additions and 5 deletions

View File

@ -313,11 +313,11 @@ def log_unsupported_webhook_event(request: HttpRequest, summary: str) -> None:
def log_exception_to_webhook_logger(request: HttpRequest, err: Exception) -> None: def log_exception_to_webhook_logger(request: HttpRequest, err: Exception) -> None:
extra = {"request": request} extra = {"request": request}
if isinstance(err, AnomalousWebhookPayloadError): if isinstance(err, AnomalousWebhookPayloadError):
webhook_anomalous_payloads_logger.exception(str(err), stack_info=True, extra=extra) webhook_anomalous_payloads_logger.exception(err, stack_info=True, extra=extra)
elif isinstance(err, UnsupportedWebhookEventTypeError): elif isinstance(err, UnsupportedWebhookEventTypeError):
webhook_unsupported_events_logger.exception(str(err), stack_info=True, extra=extra) webhook_unsupported_events_logger.exception(err, stack_info=True, extra=extra)
else: else:
webhook_logger.exception(str(err), stack_info=True, extra=extra) webhook_logger.exception(err, stack_info=True, extra=extra)
def full_webhook_client_name(raw_client_name: Optional[str] = None) -> Optional[str]: def full_webhook_client_name(raw_client_name: Optional[str] = None) -> Optional[str]:

View File

@ -414,8 +414,12 @@ class DecoratorLoggingTestCase(ZulipTestCase):
with self.assertRaisesRegex(UnsupportedWebhookEventTypeError, exception_msg): with self.assertRaisesRegex(UnsupportedWebhookEventTypeError, exception_msg):
my_webhook_raises_exception(request) my_webhook_raises_exception(request)
mock_exception.assert_called_with( mock_exception.assert_called_once()
exception_msg, stack_info=True, extra={"request": request} self.assertIsInstance(mock_exception.call_args.args[0], UnsupportedWebhookEventTypeError)
self.assertEqual(mock_exception.call_args.args[0].event_type, "test_event")
self.assertEqual(mock_exception.call_args.args[0].msg, exception_msg)
self.assertEqual(
mock_exception.call_args.kwargs, {"stack_info": True, "extra": {"request": request}}
) )
def test_authenticated_rest_api_view_with_non_webhook_view(self) -> None: def test_authenticated_rest_api_view_with_non_webhook_view(self) -> None: