47 lines
1,017 B
Python
47 lines
1,017 B
Python
"""Flask app error handlers"""
|
|
|
|
from pathlib import Path
|
|
|
|
from flask import Flask
|
|
from flask import render_template
|
|
|
|
from . import routes
|
|
|
|
# Add other HTTP error codes here
|
|
CODES = [404, 500]
|
|
|
|
|
|
class RouteErrors(routes.Routes):
|
|
"""Error handlers wrapped in Routes interface"""
|
|
|
|
def __init__(self, app: Flask) -> None:
|
|
|
|
super().__init__(app)
|
|
|
|
tmpl_dir = self.app.template_folder
|
|
if tmpl_dir is None:
|
|
return
|
|
|
|
self.tmpl = Path(__file__).parent / tmpl_dir
|
|
|
|
def add_routes(self) -> None:
|
|
"""Add all error handlers"""
|
|
|
|
for code in CODES:
|
|
self.add_handler(code)
|
|
|
|
def add_handler(self, code: int) -> None:
|
|
"""Add Flask app error handler.
|
|
Only for internal use
|
|
|
|
Args:
|
|
code (int): HTTP error code
|
|
"""
|
|
|
|
file = f'{code}.html'
|
|
|
|
if (self.tmpl / file).exists():
|
|
|
|
@self.app.errorhandler(code)
|
|
def handler(_e):
|
|
return render_template(file), code
|