Files
bricktracker
sql
views
__init__.py
add.py
admin.py
error.py
exceptions.py
index.py
instructions.py
login.py
minifigure.py
part.py
set.py
upload.py
wish.py
__init__.py
app.py
config.py
configuration.py
configuration_list.py
exceptions.py
fields.py
instructions.py
instructions_list.py
login.py
minifigure.py
minifigure_list.py
navbar.py
part.py
part_list.py
rebrickable.py
rebrickable_image.py
rebrickable_minifigures.py
rebrickable_parts.py
rebrickable_set.py
record.py
record_list.py
retired.py
retired_list.py
set.py
set_list.py
socket.py
sql.py
sql_stats.py
theme.py
theme_list.py
version.py
wish.py
wish_list.py
static
templates
.dockerignore
.env.sample
.gitignore
Dockerfile
LICENSE
README.md
__init__.py
app.py
compose.yaml
entrypoint.sh
requirements.txt
test-server.sh
BrickTracker/bricktracker/views/exceptions.py
2025-01-17 11:03:00 +01:00

47 lines
1.2 KiB
Python

from functools import wraps
import logging
from typing import Callable, ParamSpec, Tuple, Union
from werkzeug.wrappers.response import Response
from .error import error
logger = logging.getLogger(__name__)
# Decorator type hinting is hard.
# What a view can return (str or Response or (Response, xxx))
ViewReturn = Union[
str,
Response,
Tuple[str | Response, int]
]
# View signature (*arg, **kwargs -> (str or Response or (Response, xxx))
P = ParamSpec('P')
ViewCallable = Callable[P, ViewReturn]
# Return the exception template or response if an exception occured
def exception_handler(
file: str,
/,
json: bool = False,
post_redirect: str | None = None
) -> Callable[[ViewCallable], ViewCallable]:
def outer(function: ViewCallable, /) -> ViewCallable:
@wraps(function)
def wrapper(*args, **kwargs) -> ViewReturn:
try:
return function(*args, **kwargs)
# Catch SQLite errors as database errors
except Exception as e:
return error(
e,
file,
json=json,
post_redirect=post_redirect,
**kwargs,
)
return wrapper
return outer