forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_notify.py
105 lines (78 loc) · 3.29 KB
/
error_notify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from collections import defaultdict
import logging
from django.conf import settings
from django.core.mail import mail_admins
from zerver.lib.actions import internal_send_message
def format_subject(subject):
"""
Escape CR and LF characters.
"""
return subject.replace('\n', '\\n').replace('\r', '\\r')
def user_info_str(report):
if report['user_full_name'] and report['user_email']:
user_info = "%(user_full_name)s (%(user_email)s)" % (report)
else:
user_info = "Anonymous user (not logged in)"
user_info += " on %s deployment" % (report['deployment'],)
return user_info
def notify_browser_error(report):
report = defaultdict(lambda: None, report)
if settings.ERROR_BOT:
zulip_browser_error(report)
email_browser_error(report)
def email_browser_error(report):
subject = "Browser error for %s" % (user_info_str(report))
body = ("User: %(user_full_name)s <%(user_email)s> on %(deployment)s\n\n"
"Message:\n%(message)s\n\nStacktrace:\n%(stacktrace)s\n\n"
"User agent: %(user_agent)s\n"
"href: %(href)s\n"
"Server path: %(server_path)s\n"
"Deployed version: %(version)s\n"
% report)
more_info = report['more_info']
if more_info is not None:
body += "\nAdditional information:"
for (key, value) in more_info.iteritems():
body += "\n %s: %s" % (key, value)
body += "\n\nLog:\n%s" % (report['log'],)
mail_admins(subject, body)
def zulip_browser_error(report):
subject = "JS error: %s" % (report['user_email'],)
user_info = user_info_str(report)
body = "User: %s\n" % (user_info,)
body += ("Message: %(message)s\n"
% report )
internal_send_message(settings.ERROR_BOT,
"stream", "errors", format_subject(subject), body)
def notify_server_error(report):
report = defaultdict(lambda: None, report)
email_server_error(report)
if settings.ERROR_BOT:
zulip_server_error(report)
def zulip_server_error(report):
subject = '%(node)s: %(message)s' % report
stack_trace = report['stack_trace'] or "No stack trace available"
user_info = user_info_str(report)
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 += "~~~~"
internal_send_message(settings.ERROR_BOT,
"stream", "errors", format_subject(subject),
"Error generated by %s\n\n~~~~ pytb\n%s\n\n~~~~\n%s" % (
user_info, stack_trace, request_repr))
def email_server_error(report):
subject = '%(node)s: %(message)s' % (report)
user_info = user_info_str(report)
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()))
message = "Error generated by %s\n\n%s\n\n%s" % (user_info, report['stack_trace'],
request_repr)
mail_admins(format_subject(subject), message, fail_silently=True)