Docstrings and types.

This commit is contained in:
Edward Betts 2023-05-14 11:07:22 +02:00
parent 96002254ad
commit a4e847355e

View file

@ -1,13 +1,19 @@
"""Send mail to admins when there is an error."""
import logging import logging
from logging.handlers import SMTPHandler
from logging import Formatter from logging import Formatter
from flask import request from logging.handlers import SMTPHandler
import flask
PROJECT = "osm-wikidata" PROJECT = "osm-wikidata"
class MatcherSMTPHandler(SMTPHandler): 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 ( return (
f"{PROJECT} error: {record.exc_info[0].__name__}" f"{PROJECT} error: {record.exc_info[0].__name__}"
if (record.exc_info and record.exc_info[0]) if (record.exc_info and record.exc_info[0])
@ -16,12 +22,16 @@ class MatcherSMTPHandler(SMTPHandler):
class RequestFormatter(Formatter): class RequestFormatter(Formatter):
def format(self, record): """Custom request formatter."""
record.request = request
def format(self, record: logging.LogRecord) -> str:
"""Add request to log record."""
record.request = flask.request
return super().format(record) 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"): if not app.config.get("ERROR_MAIL"):
return return
formatter = RequestFormatter( formatter = RequestFormatter(