From 8c0fd5beaf2bb6238c847938f15c29bd128bbbd4 Mon Sep 17 00:00:00 2001 From: Greg Price Date: Thu, 30 Nov 2017 13:55:48 -0800 Subject: [PATCH] errors: Clean up the logic slightly. This deduplicates a little bit of logic, and also has us always put things into `report` the same way. Empirically an exception in this codepath is very rare, so we won't complicate the code by trying to salvage a lot of partial information if it happens -- just log the traceback, and try to get a minimal notification sent of the bare fact this happened. --- zerver/logging_handlers.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/zerver/logging_handlers.py b/zerver/logging_handlers.py index 4ebeb18de0..3738ca4864 100644 --- a/zerver/logging_handlers.py +++ b/zerver/logging_handlers.py @@ -63,35 +63,33 @@ class AdminNotifyHandler(logging.Handler): logging.Handler.__init__(self) def emit(self, record: logging.LogRecord) -> None: + report = {} + try: + report['node'] = platform.node() + report['host'] = platform.node() + + stack_trace = None if record.exc_info: - stack_trace = ''.join(traceback.format_exception(*record.exc_info)) # type: Optional[str] + stack_trace = ''.join(traceback.format_exception(*record.exc_info)) message = str(record.exc_info[1]) else: - stack_trace = None message = record.getMessage() if '\n' in message: # Some exception code paths in queue processors # seem to result in super-long messages stack_trace = message message = message.split('\n')[0] + report['stack_trace'] = stack_trace + report['message'] = message - report = dict( - node = platform.node(), - host = platform.node(), - message = message, - stack_trace = stack_trace, - ) if hasattr(record, "request"): add_request_metadata(report, record.request) # type: ignore # record.request is added dynamically + except Exception: - traceback.print_exc() - report = dict( - node = platform.node(), - host = platform.node(), - message = record.getMessage(), - stack_trace = "See /var/log/zulip/errors.log", - ) + report['message'] = "Exception in preparing exception report!" + logging.warning(report['message'], exc_info=True) + report['stack_trace'] = "See /var/log/zulip/errors.log" try: if settings.STAGING_ERROR_NOTIFICATIONS: