Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
ruff: Fix LOG004 `.exception()` call outside exception handlers.
Anders Kaseorg4079fa93515f
diff --git a/zerver/decorator.py b/zerver/decorator.pyindex d5d35173f8f..dd385b41213 100644--- a/zerver/decorator.py+++ b/zerver/decorator.py@@ -339,7 +339,7 @@ def log_unsupported_webhook_event(request: HttpRequest, summary: str) -> None:# really fit what a regular UnsupportedWebhookEventTypeError exception# represents.extra = {"request": request}- webhook_unsupported_events_logger.exception(summary, stack_info=True, extra=extra)+ webhook_unsupported_events_logger.error(summary, stack_info=True, extra=extra)def log_exception_to_webhook_logger(request: HttpRequest, err: Exception) -> None:@@ -354,11 +354,11 @@ def log_exception_to_webhook_logger(request: HttpRequest, err: Exception) -> Non# they are intentionally raised, and the stack_info between that# point and this one is not interesting.if isinstance(err, AnomalousWebhookPayloadError):- webhook_anomalous_payloads_logger.exception(err, extra=extra)+ webhook_anomalous_payloads_logger.error(err, exc_info=err, extra=extra)elif isinstance(err, UnsupportedWebhookEventTypeError):- webhook_unsupported_events_logger.exception(err, extra=extra)+ webhook_unsupported_events_logger.error(err, exc_info=err, extra=extra)else:- webhook_logger.exception(err, stack_info=True, extra=extra)+ webhook_logger.error(err, exc_info=err, stack_info=True, extra=extra)def full_webhook_client_name(raw_client_name: str | None = None) -> str | None:diff --git a/zerver/lib/email_mirror_server.py b/zerver/lib/email_mirror_server.pyindex 5e584b47073..512d3465b45 100644--- a/zerver/lib/email_mirror_server.py+++ b/zerver/lib/email_mirror_server.py@@ -148,7 +148,7 @@ class ZulipMessageHandler(MessageHandler):logger.debug("Dropping invalid TLS connection: %s", reason)return f"421 4.7.6 TLS error: {reason}"else:- logger.exception("SMTP session exception")+ logger.error("SMTP session exception", exc_info=error)return "500 Server error"diff --git a/zerver/lib/remote_server.py b/zerver/lib/remote_server.pyindex c24b63b6304..7da3370742f 100644--- a/zerver/lib/remote_server.py+++ b/zerver/lib/remote_server.py@@ -289,7 +289,7 @@ def maybe_mark_pushes_disabled(if isinstance(e, JsonableError):logger.warning(e.msg)else:- logger.exception("Exception communicating with %s", settings.ZULIP_SERVICES_URL)+ logger.error("Exception communicating with %s", settings.ZULIP_SERVICES_URL, exc_info=e)# An exception was thrown talking to the push bouncer. There may# be certain transient failures that we could ignore here -diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.pyindex 4a7a0059663..41148eef8e3 100644--- a/zerver/tests/test_decorators.py+++ b/zerver/tests/test_decorators.py@@ -423,18 +423,16 @@ class DecoratorLoggingTestCase(ZulipTestCase):request._body = b"{}"request.content_type = "text/plain"- with mock.patch(- "zerver.decorator.webhook_unsupported_events_logger.exception"- ) as mock_exception:+ with self.assertLogs("zulip.zerver.webhooks.unsupported") as unsupported_log:exception_msg = "The 'test_event' event isn't currently supported by the ClientName webhook; ignoring"with self.assertRaisesRegex(UnsupportedWebhookEventTypeError, exception_msg):my_webhook_raises_exception(request)- mock_exception.assert_called_once()- 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, {"extra": {"request": request}})+ self.assert_length(unsupported_log.records, 1)+ assert isinstance(unsupported_log.records[0].msg, UnsupportedWebhookEventTypeError)+ self.assertEqual(unsupported_log.records[0].msg.event_type, "test_event")+ self.assertEqual(unsupported_log.records[0].msg.msg, exception_msg)+ self.assertEqual(unsupported_log.records[0].request, request) # type: ignore[attr-defined] # added via extradef test_authenticated_rest_api_view_with_non_webhook_view(self) -> None:@authenticated_rest_api_view()diff --git a/zerver/webhooks/github/tests.py b/zerver/webhooks/github/tests.pyindex 5f5909e4419..90c563f0e2d 100644--- a/zerver/webhooks/github/tests.py+++ b/zerver/webhooks/github/tests.py@@ -702,9 +702,7 @@ A temporary team so that I can get some webhook fixtures!team=dict(name="My Team"),)- log_mock = patch("zerver.decorator.webhook_unsupported_events_logger.exception")-- with log_mock as m:+ with self.assertLogs("zulip.zerver.webhooks.unsupported") as unsupported_log:channel_message = self.send_webhook_payload(self.test_user,self.url,@@ -720,15 +718,15 @@ A temporary team so that I can get some webhook fixtures!content="Team has changes to `bogus_key1/bogus_key2` data.",)- m.assert_called_once()- msg = m.call_args[0][0]- stack_info = m.call_args[1]["stack_info"]+ self.assert_length(unsupported_log.records, 1)+ msg = unsupported_log.records[0].msg+ stack_info = unsupported_log.records[0].stack_infoself.assertIn("The 'team/edited (changes: bogus_key1/bogus_key2)' event isn't currently supported by the GitHub webhook; ignoring",msg,)- self.assertTrue(stack_info)+ self.assertIsInstance(stack_info, str)def test_discussion_answered(self) -> None:expected_message = "Niloth-p marked [comment #11460065](https://github.com/Niloth-p/webhook-tester/discussions/5#discussioncomment-11460065) as the answer:\n\n``` quote\nIf you're looking for a detailed explanation of the project structure, I'd recommend checking out our CONTRIBUTING.md file. It includes a breakdown of the different directories and files, as well as some guidelines for contributing to the project.\n```"diff --git a/zerver/worker/base.py b/zerver/worker/base.pyindex ab69cc67718..6be41727da0 100644--- a/zerver/worker/base.py+++ b/zerver/worker/base.py@@ -266,11 +266,15 @@ class QueueProcessingWorker(ABC):)if isinstance(exception, WorkerTimeoutError):scope.fingerprint = ["worker-timeout", self.queue_name]- logging.exception(exception, stack_info=True)+ logging.error(exception, exc_info=exception, stack_info=True)else:- logging.exception(- "Problem handling data on queue %s", self.queue_name, stack_info=True+ logging.error(+ "Problem handling data on queue %s",+ self.queue_name,+ exc_info=exception,+ stack_info=True,)+if not os.path.exists(settings.QUEUE_ERROR_DIR):os.mkdir(settings.QUEUE_ERROR_DIR) # nocoverage# Use 'mark_sanitized' to prevent Pysa from detecting this false positive