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
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext 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
|
|
|
"""
|
2021-02-12 08:20:45 +01:00
|
|
|
return email_subject.replace("\n", "\\n").replace("\r", "\\r")
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-12-01 01:26:37 +01:00
|
|
|
def logger_repr(report: Dict[str, Any]) -> str:
|
2020-06-14 02:57:50 +02:00
|
|
|
return "Logger {logger_name}, from module {log_module} line {log_lineno}:".format(**report)
|
2017-12-01 01:26:37 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def user_info_str(report: Dict[str, Any]) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
if report.get("user") and report["user"].get("user_full_name"):
|
2021-02-12 08:19:30 +01:00
|
|
|
user_info = "{user[user_full_name]} <{user[user_email]}> ({user[user_role]})".format(
|
|
|
|
**report
|
|
|
|
)
|
2013-11-13 19:12:22 +01:00
|
|
|
else:
|
|
|
|
user_info = "Anonymous user (not logged in)"
|
|
|
|
|
2021-01-13 20:23:57 +01:00
|
|
|
user_info += " on {host} deployment".format(**report)
|
2013-11-13 19:12:22 +01:00
|
|
|
return user_info
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-12-01 01:54:24 +01:00
|
|
|
def deployment_repr(report: Dict[str, Any]) -> str:
|
2021-02-12 08:20:45 +01:00
|
|
|
deployment = "Deployed code:\n"
|
|
|
|
for field, val in report["deployment_data"].items():
|
|
|
|
deployment += f"- {field}: {val}\n"
|
2017-08-26 01:21:04 +02:00
|
|
|
return deployment
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
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)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def email_browser_error(report: Dict[str, Any]) -> None:
|
2020-07-21 02:08:10 +02:00
|
|
|
user_info = user_info_str(report)
|
|
|
|
email_subject = f"Browser error for {user_info}"
|
|
|
|
body = f"User: {user_info}"
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2020-07-21 02:08:10 +02:00
|
|
|
body += """\
|
2020-06-14 02:57:50 +02:00
|
|
|
|
|
|
|
Message:
|
|
|
|
{message}
|
|
|
|
|
|
|
|
Stacktrace:
|
|
|
|
{stacktrace}
|
|
|
|
|
|
|
|
IP address: {ip_address}
|
|
|
|
User agent: {user_agent}
|
|
|
|
href: {href}
|
|
|
|
Server path: {server_path}
|
|
|
|
Deployed version: {version}
|
2021-02-12 08:19:30 +01:00
|
|
|
""".format(
|
|
|
|
**report
|
|
|
|
)
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
more_info = report["more_info"]
|
2013-11-13 19:12:22 +01:00
|
|
|
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-14 02:57:50 +02:00
|
|
|
body += "\n\nLog:\n{log}".format(**report)
|
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
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def zulip_browser_error(report: Dict[str, Any]) -> None:
|
2020-06-14 02:57:50 +02:00
|
|
|
email_subject = "JS error: {user_email}".format(**report)
|
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"
|
2020-06-14 02:57:50 +02:00
|
|
|
body += "Message: {message}\n".format(**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
|
2021-02-12 08:20:45 +01:00
|
|
|
errors_stream = get_stream("errors", realm)
|
2020-02-11 15:32:46 +01:00
|
|
|
|
|
|
|
internal_send_stream_message(
|
|
|
|
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
|
|
|
|
2021-02-12 08:19:30 +01: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)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def zulip_server_error(report: Dict[str, Any]) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
email_subject = "{node}: {message}".format(**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
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if report["has_request"]:
|
2020-06-14 02:57:50 +02:00
|
|
|
request_repr = """\
|
|
|
|
Request info:
|
|
|
|
~~~~
|
|
|
|
- path: {path}
|
|
|
|
- {method}: {data}
|
2021-02-12 08:19:30 +01:00
|
|
|
""".format(
|
|
|
|
**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))
|
2021-02-12 08:20:45 +01: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
|
|
|
|
2020-06-14 02:57:50 +02:00
|
|
|
message = f"""{logger_str}
|
|
|
|
Error generated by {user_info}
|
|
|
|
|
|
|
|
~~~~ pytb
|
|
|
|
{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
|
2021-02-12 08:20:45 +01:00
|
|
|
errors_stream = get_stream("errors", realm)
|
2020-02-11 15:32:46 +01:00
|
|
|
|
|
|
|
internal_send_stream_message(
|
|
|
|
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
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def email_server_error(report: Dict[str, Any]) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
email_subject = "{node}: {message}".format(**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
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if report["has_request"]:
|
2020-06-14 02:57:50 +02:00
|
|
|
request_repr = """\
|
|
|
|
Request info:
|
|
|
|
- path: {path}
|
|
|
|
- {method}: {data}
|
2021-02-12 08:19:30 +01:00
|
|
|
""".format(
|
|
|
|
**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))
|
2021-02-12 08:20:45 +01: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
|
|
|
|
2020-06-14 02:57:50 +02:00
|
|
|
message = f"""\
|
|
|
|
{logger_str}
|
|
|
|
Error generated by {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
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-01-13 20:23:57 +01:00
|
|
|
def do_report_error(type: str, report: Dict[str, Any]) -> HttpResponse:
|
2021-02-12 08:20:45 +01:00
|
|
|
if type == "browser":
|
2017-01-24 07:37:46 +01:00
|
|
|
notify_browser_error(report)
|
2021-02-12 08:20:45 +01:00
|
|
|
elif type == "server":
|
2017-01-24 07:37:46 +01:00
|
|
|
notify_server_error(report)
|
|
|
|
else:
|
|
|
|
return json_error(_("Invalid type parameter"))
|
|
|
|
return json_success()
|