From 77e47900ddc83c2f6878f99b3c3786d6ccf88800 Mon Sep 17 00:00:00 2001 From: Greg Price Date: Thu, 30 Nov 2017 14:45:45 -0800 Subject: [PATCH] errors: Stop showing a long list of Nones when there's no request. This had contributed to making the emails look broken and of uncertain accuracy. --- zerver/lib/error_notify.py | 32 +++++++++++++++++++------------- zerver/logging_handlers.py | 2 ++ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/zerver/lib/error_notify.py b/zerver/lib/error_notify.py index d6659506b2..08432cf13d 100644 --- a/zerver/lib/error_notify.py +++ b/zerver/lib/error_notify.py @@ -110,13 +110,16 @@ def zulip_server_error(report: Dict[str, Any]) -> None: user_info = user_info_str(report) deployment = deployment_repr() - request_repr = ( - "Request info:\n~~~~\n" - "- path: %(path)s\n" - "- %(method)s: %(data)s\n") % (report) - for field in ["REMOTE_ADDR", "QUERY_STRING", "SERVER_NAME"]: - request_repr += "- %s: \"%s\"\n" % (field, report.get(field.lower())) - request_repr += "~~~~" + if report['has_request']: + request_repr = ( + "Request info:\n~~~~\n" + "- path: %(path)s\n" + "- %(method)s: %(data)s\n") % (report) + for field in ["REMOTE_ADDR", "QUERY_STRING", "SERVER_NAME"]: + request_repr += "- %s: \"%s\"\n" % (field, report.get(field.lower())) + request_repr += "~~~~" + else: + request_repr = "Request info: none" message = ("Error generated by %s\n\n~~~~ pytb\n%s\n\n~~~~\n%s\n%s" % (user_info, report['stack_trace'], deployment, request_repr)) @@ -131,12 +134,15 @@ def email_server_error(report: Dict[str, Any]) -> None: user_info = user_info_str(report) deployment = deployment_repr() - request_repr = ( - "Request info:\n" - "- path: %(path)s\n" - "- %(method)s: %(data)s\n") % (report) - for field in ["REMOTE_ADDR", "QUERY_STRING", "SERVER_NAME"]: - request_repr += "- %s: \"%s\"\n" % (field, report.get(field.lower())) + if report['has_request']: + request_repr = ( + "Request info:\n" + "- path: %(path)s\n" + "- %(method)s: %(data)s\n") % (report) + for field in ["REMOTE_ADDR", "QUERY_STRING", "SERVER_NAME"]: + request_repr += "- %s: \"%s\"\n" % (field, report.get(field.lower())) + else: + request_repr = "Request info: none\n" message = ("Error generated by %s\n\n%s\n\n%s\n\n%s" % (user_info, report['stack_trace'], deployment, request_repr)) diff --git a/zerver/logging_handlers.py b/zerver/logging_handlers.py index 41910f39b4..9844e356b6 100644 --- a/zerver/logging_handlers.py +++ b/zerver/logging_handlers.py @@ -14,6 +14,8 @@ from django.views.debug import ExceptionReporter, get_exception_reporter_filter from zerver.lib.queue import queue_json_publish def add_request_metadata(report: Dict[str, Any], request: HttpRequest) -> None: + report['has_request'] = True + report['path'] = request.path report['method'] = request.method report['remote_addr'] = request.META.get('REMOTE_ADDR', None),