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: Don't double-log AnomalousWebhookPayloadError.

Alex Vandiver05d8c9d49ed9
diff --git a/zerver/decorator.py b/zerver/decorator.py
index 4616d19494e..abb2e209d74 100644
--- a/zerver/decorator.py
+++ b/zerver/decorator.py
@@ -362,7 +362,8 @@ def webhook_view(
log_exception_to_webhook_logger(request, err)
elif isinstance(err, WebhookError):
# Anything explicitly a webhook error deserves to
- # go to the webhook logs
+ # go to the webhook logs. The error middleware
+ # skips logging these exceptions a second time.
err.webhook_name = webhook_client_name
log_exception_to_webhook_logger(request, err)
elif isinstance(err, InvalidJSONError) and notify_bot_owner_on_invalid_json:
@@ -777,7 +778,8 @@ def authenticated_rest_api_view(
log_exception_to_webhook_logger(request, err)
elif isinstance(err, WebhookError):
# Anything explicitly a webhook error deserves to
- # go to the webhook logs
+ # go to the webhook logs. The error middleware
+ # skips logging these exceptions a second time.
err.webhook_name = webhook_client_name
log_exception_to_webhook_logger(request, err)
diff --git a/zerver/middleware.py b/zerver/middleware.py
index 33e814f3d6e..d6c09517863 100644
--- a/zerver/middleware.py
+++ b/zerver/middleware.py
@@ -28,7 +28,7 @@ from typing_extensions import Concatenate, ParamSpec
from zerver.lib.cache import get_remote_cache_requests, get_remote_cache_time
from zerver.lib.db import reset_queries
from zerver.lib.debug import maybe_tracemalloc_listen
-from zerver.lib.exceptions import ErrorCode, JsonableError, MissingAuthenticationError
+from zerver.lib.exceptions import ErrorCode, JsonableError, MissingAuthenticationError, WebhookError
from zerver.lib.html_to_text import get_content_description
from zerver.lib.markdown import get_markdown_requests, get_markdown_time
from zerver.lib.per_request_cache import flush_per_request_caches
@@ -384,7 +384,10 @@ class JsonErrorHandler(MiddlewareMixin):
if isinstance(exception, JsonableError):
response = json_response_from_error(exception)
- if response.status_code < 500:
+ if response.status_code < 500 or isinstance(exception, WebhookError):
+ # Webhook errors are handled in
+ # authenticated_rest_api_view / webhook_view, so we
+ # just return the response without logging further.
return response
elif RequestNotes.get_notes(request).error_format == "JSON" and not settings.TEST_SUITE:
response = json_response(res_type="error", msg=_("Internal server error"), status=500)