2017-11-16 19:51:44 +01:00
|
|
|
# System documented in https://zulip.readthedocs.io/en/latest/subsystems/logging.html
|
2017-01-24 07:28:24 +01:00
|
|
|
|
2013-11-13 19:12:22 +01:00
|
|
|
import logging
|
|
|
|
|
2017-01-24 07:28:24 +01:00
|
|
|
from collections import defaultdict
|
2013-11-13 19:12:22 +01:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.mail import mail_admins
|
2017-01-24 07:37:46 +01:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.utils.translation import ugettext as _
|
2018-05-10 19:13:36 +02:00
|
|
|
from typing import Any, Dict, Optional
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2017-05-22 23:37:15 +02:00
|
|
|
from zerver.models import get_system_bot
|
2013-11-13 19:12:22 +01:00
|
|
|
from zerver.lib.actions import internal_send_message
|
2017-01-24 07:37:46 +01:00
|
|
|
from zerver.lib.response import json_success, json_error
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def format_subject(subject: 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
|
|
|
|
2017-12-01 01:26:37 +01:00
|
|
|
def logger_repr(report: Dict[str, Any]) -> str:
|
|
|
|
return ("Logger %(logger_name)s, from module %(log_module)s line %(log_lineno)d:"
|
|
|
|
% report)
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def user_info_str(report: 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
|
|
|
|
|
2017-12-01 01:54:24 +01:00
|
|
|
def deployment_repr(report: Dict[str, Any]) -> str:
|
2017-08-26 01:21:04 +02:00
|
|
|
deployment = 'Deployed code:\n'
|
2017-12-01 01:54:24 +01:00
|
|
|
for (label, field) in [('git', 'git_described'),
|
|
|
|
('ZULIP_VERSION', 'zulip_version_const'),
|
|
|
|
('version', 'zulip_version_file'),
|
|
|
|
]:
|
|
|
|
if report[field] is not None:
|
|
|
|
deployment += '- %s: %s\n' % (label, report[field])
|
2017-08-26 01:21:04 +02:00
|
|
|
return deployment
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def notify_browser_error(report: 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)
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def email_browser_error(report: 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"
|
2017-10-04 19:27:58 +02:00
|
|
|
"IP address: %(ip_address)s\n"
|
2013-11-13 19:12:22 +01:00
|
|
|
"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:"
|
2017-09-27 10:06:17 +02:00
|
|
|
for (key, value) in more_info.items():
|
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)
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def zulip_browser_error(report: 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-05-22 23:37:15 +02:00
|
|
|
realm = get_system_bot(settings.ERROR_BOT).realm
|
2017-01-22 05:23:36 +01:00
|
|
|
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
|
|
|
|
2018-07-02 09:55:42 +02:00
|
|
|
def notify_server_error(report: Dict[str, Any], skip_error_zulip: Optional[bool]=False) -> None:
|
2013-11-13 19:12:22 +01:00
|
|
|
report = defaultdict(lambda: None, report)
|
|
|
|
email_server_error(report)
|
2018-07-02 09:55:42 +02:00
|
|
|
if settings.ERROR_BOT and not skip_error_zulip:
|
2013-11-13 19:12:22 +01:00
|
|
|
zulip_server_error(report)
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def zulip_server_error(report: 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
|
|
|
|
2017-12-01 01:26:37 +01:00
|
|
|
logger_str = logger_repr(report)
|
2013-11-13 19:12:22 +01:00
|
|
|
user_info = user_info_str(report)
|
2017-12-01 01:54:24 +01:00
|
|
|
deployment = deployment_repr(report)
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2017-11-30 23:45:45 +01:00
|
|
|
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"
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2017-12-01 01:26:37 +01:00
|
|
|
message = ("%s\nError generated by %s\n\n~~~~ pytb\n%s\n\n~~~~\n%s\n%s"
|
|
|
|
% (logger_str, user_info, report['stack_trace'], deployment, request_repr))
|
2017-11-30 23:18:16 +01:00
|
|
|
|
2017-05-22 23:37:15 +02:00
|
|
|
realm = get_system_bot(settings.ERROR_BOT).realm
|
2017-11-30 23:18:16 +01:00
|
|
|
internal_send_message(realm, settings.ERROR_BOT, "stream", "errors",
|
|
|
|
format_subject(subject), message)
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def email_server_error(report: Dict[str, Any]) -> None:
|
2013-11-13 19:12:22 +01:00
|
|
|
subject = '%(node)s: %(message)s' % (report)
|
|
|
|
|
2017-12-01 01:26:37 +01:00
|
|
|
logger_str = logger_repr(report)
|
2013-11-13 19:12:22 +01:00
|
|
|
user_info = user_info_str(report)
|
2017-12-01 01:54:24 +01:00
|
|
|
deployment = deployment_repr(report)
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2017-11-30 23:45:45 +01:00
|
|
|
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"
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2017-12-01 01:26:37 +01:00
|
|
|
message = ("%s\nError generated by %s\n\n%s\n\n%s\n\n%s"
|
|
|
|
% (logger_str, user_info, report['stack_trace'], deployment, request_repr))
|
2013-11-13 19:12:22 +01:00
|
|
|
|
|
|
|
mail_admins(format_subject(subject), message, fail_silently=True)
|
2017-01-24 07:37:46 +01:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def do_report_error(deployment_name: str, type: str, report: Dict[str, Any]) -> HttpResponse:
|
2017-01-24 07:37:46 +01:00
|
|
|
report['deployment'] = deployment_name
|
|
|
|
if type == 'browser':
|
|
|
|
notify_browser_error(report)
|
|
|
|
elif type == 'server':
|
|
|
|
notify_server_error(report)
|
|
|
|
else:
|
|
|
|
return json_error(_("Invalid type parameter"))
|
|
|
|
return json_success()
|