Files
bricktracker
migrations
sql
views
admin
__init__.py
add.py
error.py
exceptions.py
index.py
instructions.py
login.py
minifigure.py
part.py
set.py
storage.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
metadata.py
metadata_list.py
minifigure.py
minifigure_list.py
navbar.py
parser.py
part.py
part_list.py
rebrickable.py
rebrickable_image.py
rebrickable_minifigure.py
rebrickable_part.py
rebrickable_set.py
rebrickable_set_list.py
record.py
record_list.py
reload.py
retired.py
retired_list.py
set.py
set_list.py
set_owner.py
set_owner_list.py
set_purchase_location.py
set_purchase_location_list.py
set_status.py
set_status_list.py
set_storage.py
set_storage_list.py
set_tag.py
set_tag_list.py
socket.py
socket_decorator.py
sql.py
sql_counter.py
sql_migration.py
sql_migration_list.py
sql_stats.py
theme.py
theme_list.py
version.py
wish.py
wish_list.py
wish_owner.py
wish_owner_list.py
docs
static
templates
.dockerignore
.env.sample
.gitignore
CHANGELOG.md
Dockerfile
LICENSE
README.md
__init__.py
app.py
compose.legacy.yml
compose.local.yaml
compose.yaml
entrypoint.sh
requirements.txt
test-server.sh
BrickTracker/bricktracker/views/exceptions.py
2025-02-04 19:06:36 +01:00

49 lines
1.3 KiB
Python

from functools import wraps
from typing import Callable, ParamSpec, Tuple, Union
from werkzeug.wrappers.response import Response
from .error import error
# 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,
error_name: str = 'error',
**superkwargs,
) -> Callable[[ViewCallable], ViewCallable]:
def outer(function: ViewCallable, /) -> ViewCallable:
@wraps(function)
def wrapper(*args, **kwargs) -> ViewReturn:
try:
return function(*args, **kwargs)
# Handle errors
except Exception as e:
return error(
e,
file,
json=json,
post_redirect=post_redirect,
error_name=error_name,
**kwargs,
**superkwargs,
)
return wrapper
return outer