Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

middleware: Log 5xx json_errors in JsonErrorHandler.

Mateusz Manderad21a1fe47f15
diff --git a/zerver/middleware.py b/zerver/middleware.py
index 9bf43b5e866..69201b97beb 100644
--- a/zerver/middleware.py
+++ b/zerver/middleware.py
@@ -16,6 +16,7 @@ from django.shortcuts import render
from django.utils import translation
from django.utils.cache import patch_vary_headers
from django.utils.deprecation import MiddlewareMixin
+from django.utils.log import log_response
from django.utils.translation import gettext as _
from django.views.csrf import csrf_failure as html_csrf_failure
from django_scim.middleware import SCIMAuthCheckMiddleware
@@ -465,7 +466,22 @@ class JsonErrorHandler(MiddlewareMixin):
return json_unauthorized(www_authenticate="session")
if isinstance(exception, JsonableError):
- return json_response_from_error(exception)
+ response = json_response_from_error(exception)
+ if response.status_code >= 500:
+ # Here we use Django's log_response the way Django uses
+ # it normally to log error responses. However, we make the small
+ # modification of including the traceback to make the log message
+ # more helpful. log_response takes care of knowing not to duplicate
+ # the logging, so Django won't generate a second log message.
+ log_response(
+ "%s: %s",
+ response.reason_phrase,
+ request.path,
+ response=response,
+ request=request,
+ exc_info=True,
+ )
+ return response
if RequestNotes.get_notes(request).error_format == "JSON" and not settings.TEST_SUITE:
capture_exception(exception)
json_error_logger = logging.getLogger("zerver.middleware.json_error_handler")
diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py
index e95dfa44f41..9e975228a30 100644
--- a/zerver/tests/test_push_notifications.py
+++ b/zerver/tests/test_push_notifications.py
@@ -460,11 +460,9 @@ class PushBouncerNotificationTest(BouncerTestCase):
"ConnectionError while trying to connect to push notification bouncer",
502,
)
- self.assertEqual(
- error_log.output,
- [
- f"ERROR:django.request:Bad Gateway: {endpoint}",
- ],
+ self.assertIn(
+ f"ERROR:django.request:Bad Gateway: {endpoint}\nTraceback",
+ error_log.output[0],
)
with responses.RequestsMock() as resp, self.assertLogs(level="WARNING") as warn_log:
@@ -472,11 +470,11 @@ class PushBouncerNotificationTest(BouncerTestCase):
result = self.client_post(endpoint, {"token": token}, subdomain="zulip")
self.assert_json_error(result, "Received 500 from push notification bouncer", 502)
self.assertEqual(
- warn_log.output,
- [
- "WARNING:root:Received 500 from push notification bouncer",
- f"ERROR:django.request:Bad Gateway: {endpoint}",
- ],
+ warn_log.output[0],
+ "WARNING:root:Received 500 from push notification bouncer",
+ )
+ self.assertIn(
+ f"ERROR:django.request:Bad Gateway: {endpoint}\nTraceback", warn_log.output[1]
)
# Add tokens