Skip to content
Myr

Improved bad factory error handling

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent d24bb9e · Browse files · 2 files+17 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+17 added−0 removed

  • tests:+7 added−0 removed
  • other:+10 added−0 removed

2 files+17 added−0 removed

modified:flask/cli.py+10 added
31 unmodified lines
32except ImportError:33 dotenv = None343536def _called_with_wrong_args(factory, exc_info):37 exc_type, exc_value, tb = exc_info38 return exc_type is TypeError and \39 str(exc_value).startswith('%s() takes' % factory.__name__)404142class NoAppException(click.UsageError):43 """Raised if an application cannot be found or loaded."""4445
34 unmodified lines
8081 if isinstance(app, Flask):82 return app83 except TypeError:84 if not _called_with_wrong_args(app_factory, sys.exc_info()):85 raise86 raise NoAppException(87 'Detected factory "{factory}" in module "{module}", but '88 'could not call it without arguments. Use '89 '"FLASK_APP=\'{module}:{factory}(args)\'" to specify '
65 unmodified lines
155156 try:157 app = call_factory(script_info, attr, args)158 except TypeError as e:159 if not _called_with_wrong_args(attr, sys.exc_info()):160 raise161 raise NoAppException(162 '{e}\nThe factory "{app_name}" in module "{module}" could not '163 'be called with the specified arguments.'.format(164 e=e, app_name=app_name, module=module.__name__
605 unmodified lines
modified:tests/test_cli.py+7 added
142 unmodified lines
143 return Flask('appname2')144145 pytest.raises(NoAppException, find_best_app, script_info, Module)146147 class Module:148 @staticmethod149 def create_app():150 raise TypeError('bad bad factory!')151152 pytest.raises(TypeError, find_best_app, script_info, Module)153154155@pytest.mark.parametrize('value,path,result', (156 ('test', cwd, 'test'),157 ('test.py', cwd, 'test'),158 ('a/test', os.path.join(cwd, 'a'), 'test'),
326 unmodified lines