From a4e847355eeac154579bf7eea4a0393f1c2bc6e5 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 14 May 2023 11:07:22 +0200 Subject: [PATCH] Docstrings and types. --- matcher/error_mail.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/matcher/error_mail.py b/matcher/error_mail.py index a7a6563..6e8c888 100644 --- a/matcher/error_mail.py +++ b/matcher/error_mail.py @@ -1,13 +1,19 @@ +"""Send mail to admins when there is an error.""" + import logging -from logging.handlers import SMTPHandler from logging import Formatter -from flask import request +from logging.handlers import SMTPHandler + +import flask PROJECT = "osm-wikidata" class MatcherSMTPHandler(SMTPHandler): - def getSubject(self, record): # noqa: N802 + """Custom SMTP handler to change subject line.""" + + def getSubject(self, record: logging.LogRecord) -> str: # noqa: N802 + """Return subject line for error mail.""" return ( f"{PROJECT} error: {record.exc_info[0].__name__}" if (record.exc_info and record.exc_info[0]) @@ -16,12 +22,16 @@ class MatcherSMTPHandler(SMTPHandler): class RequestFormatter(Formatter): - def format(self, record): - record.request = request + """Custom request formatter.""" + + def format(self, record: logging.LogRecord) -> str: + """Add request to log record.""" + record.request = flask.request return super().format(record) -def setup_error_mail(app): +def setup_error_mail(app: flask.Flask) -> None: + """Configure logging to catch errors and email them.""" if not app.config.get("ERROR_MAIL"): return formatter = RequestFormatter(