Merge pull request #4348 from Yourun-proger/fix_msg

Fix error message for `after_this_request` when used outside request context
This commit is contained in:
David Lord 2021-12-22 17:49:30 -08:00 committed by GitHub
commit 6389c07530
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 4 deletions

View file

@ -15,6 +15,8 @@ Unreleased
- ``app.json_encoder`` and ``json_decoder`` are only passed to
``dumps`` and ``loads`` if they have custom behavior. This improves
performance, mainly on PyPy. :issue:`4349`
- Clearer error message when ``after_this_request`` is used outside a
request context. :issue:`4333`
Version 2.0.2

View file

@ -130,7 +130,15 @@ def after_this_request(f: AfterRequestCallable) -> AfterRequestCallable:
.. versionadded:: 0.9
"""
_request_ctx_stack.top._after_request_functions.append(f)
top = _request_ctx_stack.top
if top is None:
raise RuntimeError(
"This decorator can only be used when a request context is"
" active, such as within a view function."
)
top._after_request_functions.append(f)
return f
@ -159,12 +167,13 @@ def copy_current_request_context(f: t.Callable) -> t.Callable:
.. versionadded:: 0.10
"""
top = _request_ctx_stack.top
if top is None:
raise RuntimeError(
"This decorator can only be used at local scopes "
"when a request context is on the stack. For instance within "
"view functions."
"This decorator can only be used when a request context is"
" active, such as within a view function."
)
reqctx = top.copy()
def wrapper(*args, **kwargs):