From 53c57cf002f951befd99ab14603fdb1c5e9bf3da Mon Sep 17 00:00:00 2001 From: Greg Price Date: Thu, 1 Mar 2018 14:55:37 -0800 Subject: [PATCH] errors: Include request info on error mails for JSON routes too. When our code raises an exception and Django converts it to a 500 response (in django.core.handlers.exception.handle_uncaught_exception), it attaches the request to the log record, and we use this in our AdminNotifyHandler to include data like the user and the URL path in the error email sent to admins. On this line, when our code raises an exception but we've decided (in `TagRequests`) to format any errors as JSON errors, we suppress the exception so we have to generate the log record ourselves. Attach the request here, just like Django does when we let it do the job. This still isn't an awesome solution, in that there are lots of other places where we call `logging.error` or `logging.exception` while inside a request; this just covers one of them. This is one of the most common, though, so it's a start. --- zerver/middleware.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zerver/middleware.py b/zerver/middleware.py index 595500c8dd..746eb31fba 100644 --- a/zerver/middleware.py +++ b/zerver/middleware.py @@ -278,7 +278,7 @@ class JsonErrorHandler(MiddlewareMixin): if isinstance(exception, JsonableError): return json_response_from_error(exception) if request.error_format == "JSON": - logging.error(traceback.format_exc()) + logging.error(traceback.format_exc(), extra=dict(request=request)) return json_error(_("Internal server error"), status=500) return None