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
|
|
|
from collections import defaultdict
|
2020-06-13 01:57:21 +02:00
|
|
|
from typing import Any, Dict
|
2020-06-11 00:54:34 +02:00
|
|
|
|
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 _
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2018-10-19 23:53:33 +02:00
|
|
|
from zerver.filters import clean_data_from_query_parameters
|
2020-02-11 15:32:46 +01:00
|
|
|
from zerver.lib.actions import internal_send_stream_message
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.response import json_error, json_success
|
|
|
|
from zerver.models import get_stream, get_system_bot
|
|
|
|
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2018-12-23 18:52:06 +01:00
|
|
|
def format_email_subject(email_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
|
|
|
"""
|
2018-12-23 18:52:06 +01:00
|
|
|
return email_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:"
|
2019-04-20 01:00:46 +02:00
|
|
|
% dict(report))
|
2017-12-01 01:26:37 +01:00
|
|
|
|
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']:
|
2019-04-20 01:00:46 +02:00
|
|
|
user_info = "%(user_full_name)s (%(user_email)s)" % dict(report)
|
2013-11-13 19:12:22 +01:00
|
|
|
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'
|
2020-05-07 00:20:16 +02:00
|
|
|
for field, val in report['deployment_data'].items():
|
2020-06-10 06:41:04 +02:00
|
|
|
deployment += f'- {field}: {val}\n'
|
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:
|
2020-06-10 06:41:04 +02:00
|
|
|
email_subject = f"Browser error for {user_info_str(report)}"
|
2013-11-13 19:12:22 +01:00
|
|
|
|
|
|
|
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"
|
2019-04-20 01:00:46 +02:00
|
|
|
% dict(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():
|
2020-06-10 06:41:04 +02:00
|
|
|
body += f"\n {key}: {value}"
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2020-06-10 06:41:04 +02:00
|
|
|
body += "\n\nLog:\n{}".format(report['log'])
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2018-12-23 18:52:06 +01:00
|
|
|
mail_admins(email_subject, body)
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def zulip_browser_error(report: Dict[str, Any]) -> None:
|
2020-06-10 06:41:04 +02:00
|
|
|
email_subject = "JS error: {}".format(report['user_email'])
|
2013-11-13 19:12:22 +01:00
|
|
|
|
|
|
|
user_info = user_info_str(report)
|
|
|
|
|
2020-06-10 06:41:04 +02:00
|
|
|
body = f"User: {user_info}\n"
|
2013-12-12 18:44:39 +01:00
|
|
|
body += ("Message: %(message)s\n"
|
2019-04-20 01:00:46 +02:00
|
|
|
% dict(report))
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2020-02-11 15:32:46 +01:00
|
|
|
error_bot = get_system_bot(settings.ERROR_BOT)
|
|
|
|
realm = error_bot.realm
|
|
|
|
errors_stream = get_stream('errors', realm)
|
|
|
|
|
|
|
|
internal_send_stream_message(
|
|
|
|
realm,
|
|
|
|
error_bot,
|
|
|
|
errors_stream,
|
|
|
|
format_email_subject(email_subject),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
body,
|
2020-02-11 15:32:46 +01:00
|
|
|
)
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2020-06-13 01:57:21 +02:00
|
|
|
def notify_server_error(report: Dict[str, Any], skip_error_zulip: 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:
|
2019-04-20 01:00:46 +02:00
|
|
|
email_subject = '%(node)s: %(message)s' % dict(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"
|
2019-04-20 01:00:46 +02:00
|
|
|
"- %(method)s: %(data)s\n") % dict(report)
|
2017-11-30 23:45:45 +01:00
|
|
|
for field in ["REMOTE_ADDR", "QUERY_STRING", "SERVER_NAME"]:
|
2018-10-31 18:55:31 +01:00
|
|
|
val = report.get(field.lower())
|
2018-10-19 23:53:33 +02:00
|
|
|
if field == "QUERY_STRING":
|
2018-10-31 18:55:31 +01:00
|
|
|
val = clean_data_from_query_parameters(str(val))
|
2020-06-10 06:41:04 +02:00
|
|
|
request_repr += f"- {field}: \"{val}\"\n"
|
2017-11-30 23:45:45 +01:00
|
|
|
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
|
|
|
|
2020-02-11 15:32:46 +01:00
|
|
|
error_bot = get_system_bot(settings.ERROR_BOT)
|
|
|
|
realm = error_bot.realm
|
|
|
|
errors_stream = get_stream('errors', realm)
|
|
|
|
|
|
|
|
internal_send_stream_message(
|
|
|
|
realm,
|
|
|
|
error_bot,
|
|
|
|
errors_stream,
|
|
|
|
format_email_subject(email_subject),
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
message,
|
2020-02-11 15:32:46 +01:00
|
|
|
)
|
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:
|
2019-04-20 01:00:46 +02:00
|
|
|
email_subject = '%(node)s: %(message)s' % dict(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"
|
|
|
|
"- path: %(path)s\n"
|
2019-04-20 01:00:46 +02:00
|
|
|
"- %(method)s: %(data)s\n") % dict(report)
|
2017-11-30 23:45:45 +01:00
|
|
|
for field in ["REMOTE_ADDR", "QUERY_STRING", "SERVER_NAME"]:
|
2018-10-31 18:55:31 +01:00
|
|
|
val = report.get(field.lower())
|
2018-10-19 23:53:33 +02:00
|
|
|
if field == "QUERY_STRING":
|
2018-10-31 18:55:31 +01:00
|
|
|
val = clean_data_from_query_parameters(str(val))
|
2020-06-10 06:41:04 +02:00
|
|
|
request_repr += f"- {field}: \"{val}\"\n"
|
2017-11-30 23:45:45 +01:00
|
|
|
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
|
|
|
|
2018-12-23 18:52:06 +01:00
|
|
|
mail_admins(format_email_subject(email_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()
|