2016-03-11 10:57:29 +01:00
|
|
|
from __future__ import absolute_import
|
2013-11-13 19:12:22 +01:00
|
|
|
from collections import defaultdict
|
2016-06-06 00:47:24 +02:00
|
|
|
from typing import Any
|
2013-11-13 19:12:22 +01:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.mail import mail_admins
|
|
|
|
|
2017-01-22 05:23:36 +01:00
|
|
|
from zerver.models import get_user_profile_by_email
|
2013-11-13 19:12:22 +01:00
|
|
|
from zerver.lib.actions import internal_send_message
|
2016-06-06 00:47:24 +02:00
|
|
|
from typing import Dict
|
2016-03-11 10:57:29 +01:00
|
|
|
import six
|
2013-11-13 19:12:22 +01:00
|
|
|
|
|
|
|
def format_subject(subject):
|
2016-06-06 00:47:24 +02:00
|
|
|
# type: (str) -> str
|
2013-11-13 19:12:22 +01:00
|
|
|
"""
|
2013-11-22 18:37:21 +01:00
|
|
|
Escape CR and LF characters.
|
2013-11-13 19:12:22 +01:00
|
|
|
"""
|
2013-11-22 18:37:21 +01:00
|
|
|
return subject.replace('\n', '\\n').replace('\r', '\\r')
|
2013-11-13 19:12:22 +01:00
|
|
|
|
|
|
|
def user_info_str(report):
|
2016-06-06 00:47:24 +02:00
|
|
|
# type: (Dict[str, Any]) -> str
|
2013-11-13 19:12:22 +01:00
|
|
|
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):
|
2016-06-06 00:47:24 +02:00
|
|
|
# type: (Dict[str, Any]) -> None
|
2013-11-13 19:12:22 +01:00
|
|
|
report = defaultdict(lambda: None, report)
|
|
|
|
if settings.ERROR_BOT:
|
|
|
|
zulip_browser_error(report)
|
|
|
|
email_browser_error(report)
|
|
|
|
|
|
|
|
def email_browser_error(report):
|
2016-06-06 00:47:24 +02:00
|
|
|
# type: (Dict[str, Any]) -> None
|
2013-11-13 19:12:22 +01:00
|
|
|
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"
|
2016-11-30 22:49:02 +01:00
|
|
|
% (report))
|
2013-11-13 19:12:22 +01:00
|
|
|
|
|
|
|
more_info = report['more_info']
|
|
|
|
if more_info is not None:
|
|
|
|
body += "\nAdditional information:"
|
2016-03-11 10:57:29 +01:00
|
|
|
for (key, value) in six.iteritems(more_info):
|
2013-11-13 19:12:22 +01:00
|
|
|
body += "\n %s: %s" % (key, value)
|
|
|
|
|
|
|
|
body += "\n\nLog:\n%s" % (report['log'],)
|
|
|
|
|
|
|
|
mail_admins(subject, body)
|
|
|
|
|
|
|
|
def zulip_browser_error(report):
|
2016-06-06 00:47:24 +02:00
|
|
|
# type: (Dict[str, Any]) -> None
|
2013-11-13 19:12:22 +01:00
|
|
|
subject = "JS error: %s" % (report['user_email'],)
|
|
|
|
|
|
|
|
user_info = user_info_str(report)
|
|
|
|
|
2016-11-30 22:49:02 +01:00
|
|
|
body = "User: %s\n" % (user_info,)
|
2013-12-12 18:44:39 +01:00
|
|
|
body += ("Message: %(message)s\n"
|
2016-12-02 06:15:18 +01:00
|
|
|
% (report))
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2017-01-22 05:23:36 +01:00
|
|
|
realm = get_user_profile_by_email(settings.ERROR_BOT).realm
|
|
|
|
internal_send_message(realm, settings.ERROR_BOT,
|
2016-12-03 00:04:17 +01:00
|
|
|
"stream", "errors", format_subject(subject), body)
|
2013-11-13 19:12:22 +01:00
|
|
|
|
|
|
|
def notify_server_error(report):
|
2016-06-06 00:47:24 +02:00
|
|
|
# type: (Dict[str, Any]) -> None
|
2013-11-13 19:12:22 +01:00
|
|
|
report = defaultdict(lambda: None, report)
|
|
|
|
email_server_error(report)
|
|
|
|
if settings.ERROR_BOT:
|
|
|
|
zulip_server_error(report)
|
|
|
|
|
|
|
|
def zulip_server_error(report):
|
2016-06-06 00:47:24 +02:00
|
|
|
# type: (Dict[str, Any]) -> None
|
2016-11-30 22:49:02 +01:00
|
|
|
subject = '%(node)s: %(message)s' % (report)
|
2013-11-13 19:12:22 +01:00
|
|
|
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 += "~~~~"
|
|
|
|
|
2017-01-22 05:23:36 +01:00
|
|
|
realm = get_user_profile_by_email(settings.ERROR_BOT).realm
|
|
|
|
internal_send_message(realm, settings.ERROR_BOT,
|
2016-12-02 08:15:16 +01:00
|
|
|
"stream", "errors", format_subject(subject),
|
|
|
|
"Error generated by %s\n\n~~~~ pytb\n%s\n\n~~~~\n%s" % (
|
2017-01-24 06:02:39 +01:00
|
|
|
user_info, stack_trace, request_repr))
|
2013-11-13 19:12:22 +01:00
|
|
|
|
|
|
|
def email_server_error(report):
|
2016-06-06 00:47:24 +02:00
|
|
|
# type: (Dict[str, Any]) -> None
|
2013-11-13 19:12:22 +01:00
|
|
|
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)
|