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 - ``app.json_encoder`` and ``json_decoder`` are only passed to
``dumps`` and ``loads`` if they have custom behavior. This improves ``dumps`` and ``loads`` if they have custom behavior. This improves
performance, mainly on PyPy. :issue:`4349` 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 Version 2.0.2

View file

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