Skip to content

Commit

Permalink
[web] allow to inline constant callables in handle_errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjuhrich committed Aug 10, 2023
1 parent cbc29d1 commit 73b4e31
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 5 additions & 2 deletions web/blueprints/helpers/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def flash_and_wrap_errors() -> t.Iterator[None]:
@contextmanager
# TODO rename to „wrap_errors“; `handle` suggests „I'll deal with everything“, which is incorrect
def handle_errors(
error_response: t.Callable[[], ResponseReturnValue] | None = None,
error_response: t.Callable[[], ResponseReturnValue]
| ResponseReturnValue
| None = None,
) -> t.Iterator[SessionTransaction]:
"""Wraps errors as `PycroftErrors` and turns them into a flash message.
Expand All @@ -68,7 +70,8 @@ def default_response(): return render_template("template.html")
with cm as n:
yield n
except PycroftException:
abort(make_response(error_response()))
resp = error_response() if callable(error_response) else error_response
abort(make_response(resp))


def exception_flash_message(e: PycroftException) -> str:
Expand Down
4 changes: 1 addition & 3 deletions web/blueprints/user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,10 +1230,8 @@ def member_request_delete(pre_member_id: int):
def member_request_finish(pre_member_id: int):
prm = get_pre_member_or_404(pre_member_id)

def default_response():
return redirect(url_for(".member_request_edit", pre_member_id=prm.id))
default_response = redirect(url_for(".member_request_edit", pre_member_id=prm.id))

# TODO make `error_response` accept non-callable instead
with handle_errors(error_response=default_response), session.session.begin_nested():
user = finish_member_request(
prm, processor=current_user, ignore_similar_name=True
Expand Down

0 comments on commit 73b4e31

Please sign in to comment.