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
|
2023-04-11 19:51:14 +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.core.mail import mail_admins
|
|
|
|
|
2018-10-19 23:53:33 +02:00
|
|
|
from zerver.filters import clean_data_from_query_parameters
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2023-04-11 19:51:14 +02:00
|
|
|
def do_report_error(report: Dict[str, Any]) -> None:
|
|
|
|
report = defaultdict(lambda: None, report)
|
|
|
|
|
2023-04-10 21:27:04 +02:00
|
|
|
topic = "{node}: {message}".format(**report).replace("\n", "\\n").replace("\r", "\\r")
|
2017-12-01 01:26:37 +01:00
|
|
|
|
2023-04-10 21:27:04 +02:00
|
|
|
logger_str = "Logger {logger_name}, from module {log_module} line {log_lineno}:".format(
|
|
|
|
**report
|
|
|
|
)
|
2021-02-12 08:19:30 +01:00
|
|
|
|
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
|
|
|
|
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"
|
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:
|
2023-04-10 21:27:04 +02:00
|
|
|
request_repr = "Request info: none\n"
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2023-04-11 19:51:14 +02:00
|
|
|
message = f"""\
|
2023-04-10 21:27:04 +02:00
|
|
|
{logger_str}
|
2020-06-14 02:57:50 +02:00
|
|
|
Error generated by {user_info}
|
|
|
|
|
|
|
|
{report['stack_trace']}
|
|
|
|
|
|
|
|
{deployment}
|
2020-02-11 15:32:46 +01:00
|
|
|
|
2023-04-10 21:27:04 +02:00
|
|
|
{request_repr}"""
|
2013-11-13 19:12:22 +01:00
|
|
|
|
2023-04-11 19:51:14 +02:00
|
|
|
mail_admins(topic, message, fail_silently=True)
|