Skip to content

Commit d89d0d1

Browse files
committed
Improve documentation
* document InvalidCommand error class * re-raise the TypeError you get from handler() when the arguments do not match with the name of the actual failed procedure, since Python does not.
1 parent d254683 commit d89d0d1

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

docs/index.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,35 @@ The commands will then be available::
569569
populate Populate database with default data
570570
recreate Recreates database tables (same as issuing 'drop' and then 'create')
571571

572+
Error handling
573+
--------------
574+
575+
Users do not like to see stack traces, but developers want them for bug reports.
576+
577+
Therefore, ``flask.ext.script.command`` provides an `InvalidCommand` error
578+
class which is not supposed to print a stack trace when reported.
579+
580+
In your command handler:
581+
582+
from flask.ext.script.command import InvalidCommand
583+
584+
[… if some command verification fails …]
585+
class MyCommand(Command):
586+
def run(self, foo=None,bar=None):
587+
if foo and bar:
588+
raise InvalidCommand("Options foo and bar are incompatible")
589+
590+
In your main loop:
591+
592+
try:
593+
MyManager().run()
594+
except InvalidCommand as err:
595+
print(err, file=sys.stderr)
596+
sys.exit(1)
597+
598+
This way, you maintain interoperability if some plug-in code supplies
599+
Flask-Script hooks you'd like to use, or vice versa.
600+
572601
Accessing local proxies
573602
-----------------------
574603

flask_script/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,12 @@ def handle(self, prog, args=None):
379379

380380
if handle is last_func and getattr(last_func, 'capture_all_args', False):
381381
args.append(remaining_args)
382-
res = handle(*args, **config)
382+
try:
383+
res = handle(*args, **config)
384+
except TypeError as err:
385+
err.args = ("{}: {}".format(handle,str(err)),)
386+
raise
387+
383388
args = [res]
384389

385390
assert not kwargs

flask_script/commands.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,18 @@
1616

1717

1818
class InvalidCommand(Exception):
19+
"""\
20+
This is a generic error for "bad" commands.
21+
It is not used in Flask-Script itself, but you should throw
22+
this error (or one derived from it) in your command handlers,
23+
and your main code should display this error's message without
24+
a stack trace.
25+
26+
This way, we maintain interoperability if some other plug-in code
27+
supplies Flask-Script hooks.
28+
"""
1929
pass
2030

21-
2231
class Group(object):
2332
"""
2433
Stores argument groups and mutually exclusive groups for

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
setup(
2929
name='Flask-Script',
30-
version='2.0.2',
30+
version='2.0.3',
3131
url='http://github.com/smurfix/flask-script',
3232
license='BSD',
3333
author='Dan Jacob',

0 commit comments

Comments
 (0)