forked from orbit-oss/flask
Merge pull request #4283 from pallets/except-chain
use exception chaining
This commit is contained in:
commit
34ff7d73a7
5 changed files with 21 additions and 20 deletions
|
|
@ -1621,7 +1621,7 @@ class Flask(Scaffold):
|
||||||
except ImportError:
|
except ImportError:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
"Install Flask with the 'async' extra in order to use async views."
|
"Install Flask with the 'async' extra in order to use async views."
|
||||||
)
|
) from None
|
||||||
|
|
||||||
# Check that Werkzeug isn't using its fallback ContextVar class.
|
# Check that Werkzeug isn't using its fallback ContextVar class.
|
||||||
if ContextVar.__module__ == "werkzeug.local":
|
if ContextVar.__module__ == "werkzeug.local":
|
||||||
|
|
@ -1727,7 +1727,7 @@ class Flask(Scaffold):
|
||||||
" response. The return type must be a string,"
|
" response. The return type must be a string,"
|
||||||
" dict, tuple, Response instance, or WSGI"
|
" dict, tuple, Response instance, or WSGI"
|
||||||
f" callable, but it was a {type(rv).__name__}."
|
f" callable, but it was a {type(rv).__name__}."
|
||||||
).with_traceback(sys.exc_info()[2])
|
).with_traceback(sys.exc_info()[2]) from None
|
||||||
else:
|
else:
|
||||||
raise TypeError(
|
raise TypeError(
|
||||||
"The view function did not return a valid"
|
"The view function did not return a valid"
|
||||||
|
|
|
||||||
|
|
@ -69,15 +69,16 @@ def find_best_app(script_info, module):
|
||||||
|
|
||||||
if isinstance(app, Flask):
|
if isinstance(app, Flask):
|
||||||
return app
|
return app
|
||||||
except TypeError:
|
except TypeError as e:
|
||||||
if not _called_with_wrong_args(app_factory):
|
if not _called_with_wrong_args(app_factory):
|
||||||
raise
|
raise
|
||||||
|
|
||||||
raise NoAppException(
|
raise NoAppException(
|
||||||
f"Detected factory {attr_name!r} in module {module.__name__!r},"
|
f"Detected factory {attr_name!r} in module {module.__name__!r},"
|
||||||
" but could not call it without arguments. Use"
|
" but could not call it without arguments. Use"
|
||||||
f" \"FLASK_APP='{module.__name__}:{attr_name}(args)'\""
|
f" \"FLASK_APP='{module.__name__}:{attr_name}(args)'\""
|
||||||
" to specify arguments."
|
" to specify arguments."
|
||||||
)
|
) from e
|
||||||
|
|
||||||
raise NoAppException(
|
raise NoAppException(
|
||||||
"Failed to find Flask application or factory in module"
|
"Failed to find Flask application or factory in module"
|
||||||
|
|
@ -161,7 +162,7 @@ def find_app_by_string(script_info, module, app_name):
|
||||||
except SyntaxError:
|
except SyntaxError:
|
||||||
raise NoAppException(
|
raise NoAppException(
|
||||||
f"Failed to parse {app_name!r} as an attribute name or function call."
|
f"Failed to parse {app_name!r} as an attribute name or function call."
|
||||||
)
|
) from None
|
||||||
|
|
||||||
if isinstance(expr, ast.Name):
|
if isinstance(expr, ast.Name):
|
||||||
name = expr.id
|
name = expr.id
|
||||||
|
|
@ -184,7 +185,7 @@ def find_app_by_string(script_info, module, app_name):
|
||||||
# message with the full expression instead.
|
# message with the full expression instead.
|
||||||
raise NoAppException(
|
raise NoAppException(
|
||||||
f"Failed to parse arguments as literal values: {app_name!r}."
|
f"Failed to parse arguments as literal values: {app_name!r}."
|
||||||
)
|
) from None
|
||||||
else:
|
else:
|
||||||
raise NoAppException(
|
raise NoAppException(
|
||||||
f"Failed to parse {app_name!r} as an attribute name or function call."
|
f"Failed to parse {app_name!r} as an attribute name or function call."
|
||||||
|
|
@ -192,17 +193,17 @@ def find_app_by_string(script_info, module, app_name):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
attr = getattr(module, name)
|
attr = getattr(module, name)
|
||||||
except AttributeError:
|
except AttributeError as e:
|
||||||
raise NoAppException(
|
raise NoAppException(
|
||||||
f"Failed to find attribute {name!r} in {module.__name__!r}."
|
f"Failed to find attribute {name!r} in {module.__name__!r}."
|
||||||
)
|
) from e
|
||||||
|
|
||||||
# If the attribute is a function, call it with any args and kwargs
|
# If the attribute is a function, call it with any args and kwargs
|
||||||
# to get the real application.
|
# to get the real application.
|
||||||
if inspect.isfunction(attr):
|
if inspect.isfunction(attr):
|
||||||
try:
|
try:
|
||||||
app = call_factory(script_info, attr, args, kwargs)
|
app = call_factory(script_info, attr, args, kwargs)
|
||||||
except TypeError:
|
except TypeError as e:
|
||||||
if not _called_with_wrong_args(attr):
|
if not _called_with_wrong_args(attr):
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
@ -210,7 +211,7 @@ def find_app_by_string(script_info, module, app_name):
|
||||||
f"The factory {app_name!r} in module"
|
f"The factory {app_name!r} in module"
|
||||||
f" {module.__name__!r} could not be called with the"
|
f" {module.__name__!r} could not be called with the"
|
||||||
" specified arguments."
|
" specified arguments."
|
||||||
)
|
) from e
|
||||||
else:
|
else:
|
||||||
app = attr
|
app = attr
|
||||||
|
|
||||||
|
|
@ -257,16 +258,15 @@ def locate_app(script_info, module_name, app_name, raise_if_not_found=True):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
__import__(module_name)
|
__import__(module_name)
|
||||||
except ImportError:
|
except ImportError as e:
|
||||||
# Reraise the ImportError if it occurred within the imported module.
|
# Reraise the ImportError if it occurred within the imported module.
|
||||||
# Determine this by checking whether the trace has a depth > 1.
|
# Determine this by checking whether the trace has a depth > 1.
|
||||||
if sys.exc_info()[2].tb_next:
|
if sys.exc_info()[2].tb_next:
|
||||||
raise NoAppException(
|
raise NoAppException(
|
||||||
f"While importing {module_name!r}, an ImportError was"
|
f"While importing {module_name!r}, an ImportError was raised."
|
||||||
f" raised:\n\n{traceback.format_exc()}"
|
) from e
|
||||||
)
|
|
||||||
elif raise_if_not_found:
|
elif raise_if_not_found:
|
||||||
raise NoAppException(f"Could not import {module_name!r}.")
|
raise NoAppException(f"Could not import {module_name!r}.") from e
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
@ -725,7 +725,7 @@ class CertParamType(click.ParamType):
|
||||||
"Using ad-hoc certificates requires the cryptography library.",
|
"Using ad-hoc certificates requires the cryptography library.",
|
||||||
ctx,
|
ctx,
|
||||||
param,
|
param,
|
||||||
)
|
) from None
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -83,10 +83,11 @@ def attach_enctype_error_multidict(request):
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
try:
|
try:
|
||||||
return oldcls.__getitem__(self, key)
|
return oldcls.__getitem__(self, key)
|
||||||
except KeyError:
|
except KeyError as e:
|
||||||
if key not in request.form:
|
if key not in request.form:
|
||||||
raise
|
raise
|
||||||
raise DebugFilesKeyError(request, key)
|
|
||||||
|
raise DebugFilesKeyError(request, key) from e
|
||||||
|
|
||||||
newcls.__name__ = oldcls.__name__
|
newcls.__name__ = oldcls.__name__
|
||||||
newcls.__module__ = oldcls.__module__
|
newcls.__module__ = oldcls.__module__
|
||||||
|
|
|
||||||
|
|
@ -715,7 +715,7 @@ class Scaffold:
|
||||||
f"'{code_or_exception}' is not a recognized HTTP error"
|
f"'{code_or_exception}' is not a recognized HTTP error"
|
||||||
" code. Use a subclass of HTTPException with that code"
|
" code. Use a subclass of HTTPException with that code"
|
||||||
" instead."
|
" instead."
|
||||||
)
|
) from None
|
||||||
|
|
||||||
self.error_handler_spec[None][code][exc_class] = t.cast(
|
self.error_handler_spec[None][code][exc_class] = t.cast(
|
||||||
"ErrorHandlerCallable[Exception]", f
|
"ErrorHandlerCallable[Exception]", f
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ except ImportError:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
"Signalling support is unavailable because the blinker"
|
"Signalling support is unavailable because the blinker"
|
||||||
" library is not installed."
|
" library is not installed."
|
||||||
)
|
) from None
|
||||||
|
|
||||||
connect = connect_via = connected_to = temporarily_connected_to = _fail
|
connect = connect_via = connected_to = temporarily_connected_to = _fail
|
||||||
disconnect = _fail
|
disconnect = _fail
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue