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.
This commit is contained in:
Greg Price
2017-11-30 13:55:48 -08:00
committed by Steve Howell
parent b15231dfc2
commit 8c0fd5beaf

View File

@@ -63,35 +63,33 @@ class AdminNotifyHandler(logging.Handler):
logging.Handler.__init__(self) logging.Handler.__init__(self)
def emit(self, record: logging.LogRecord) -> None: def emit(self, record: logging.LogRecord) -> None:
report = {}
try: try:
report['node'] = platform.node()
report['host'] = platform.node()
stack_trace = None
if record.exc_info: 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]) message = str(record.exc_info[1])
else: else:
stack_trace = None
message = record.getMessage() message = record.getMessage()
if '\n' in message: if '\n' in message:
# Some exception code paths in queue processors # Some exception code paths in queue processors
# seem to result in super-long messages # seem to result in super-long messages
stack_trace = message stack_trace = message
message = message.split('\n')[0] 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"): if hasattr(record, "request"):
add_request_metadata(report, record.request) # type: ignore # record.request is added dynamically add_request_metadata(report, record.request) # type: ignore # record.request is added dynamically
except Exception: except Exception:
traceback.print_exc() report['message'] = "Exception in preparing exception report!"
report = dict( logging.warning(report['message'], exc_info=True)
node = platform.node(), report['stack_trace'] = "See /var/log/zulip/errors.log"
host = platform.node(),
message = record.getMessage(),
stack_trace = "See /var/log/zulip/errors.log",
)
try: try:
if settings.STAGING_ERROR_NOTIFICATIONS: if settings.STAGING_ERROR_NOTIFICATIONS: