diff --git a/django/core/management/base.py b/django/core/management/base.py
index 6e06991..9031ce4 100644
a
|
b
|
class BaseCommand(object):
|
266 | 266 | if self.output_transaction: |
267 | 267 | self.stdout.write('\n' + self.style.SQL_KEYWORD("COMMIT;")) |
268 | 268 | except CommandError as e: |
| 269 | if options.get('raise_on_error', False): |
| 270 | raise |
269 | 271 | if show_traceback: |
270 | | traceback.print_exc() |
| 272 | self.stderr.write(traceback.format_exc()) |
271 | 273 | else: |
272 | 274 | self.stderr.write('Error: %s' % e) |
273 | 275 | sys.exit(1) |
diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
index 0ea8252..4f13fa9 100644
a
|
b
|
To call a management command from code use ``call_command``.
|
1468 | 1468 | ``**options`` |
1469 | 1469 | named options accepted on the command-line. |
1470 | 1470 | |
| 1471 | .. versionadded:: 1.5 |
| 1472 | |
| 1473 | If you pass ``raise_on_error=True`` as keyword argument to ``call_command``, |
| 1474 | the command will not call ``sys.exit(1)`` when a ``CommandError`` is raised, but |
| 1475 | will simply let ``CommandError`` propagate. This allows you to catch the |
| 1476 | exception, for example when you test a command. |
| 1477 | |
1471 | 1478 | Examples:: |
1472 | 1479 | |
1473 | 1480 | from django.core import management |
| 1481 | from django.core.management.base import CommandError |
| 1482 | |
1474 | 1483 | management.call_command('flush', verbosity=0, interactive=False) |
1475 | 1484 | management.call_command('loaddata', 'test_data', verbosity=0) |
| 1485 | try: |
| 1486 | management.call_command('flush', interactive=False, raise_on_error=True) |
| 1487 | except CommandError: |
| 1488 | ... |
diff --git a/tests/modeltests/user_commands/management/commands/dance.py b/tests/modeltests/user_commands/management/commands/dance.py
index 4ad5579..c1b5cd9 100644
a
|
b
|
|
1 | 1 | from optparse import make_option |
2 | 2 | |
3 | | from django.core.management.base import BaseCommand |
| 3 | from django.core.management.base import BaseCommand, CommandError |
4 | 4 | |
5 | 5 | |
6 | 6 | class Command(BaseCommand): |
… |
… |
class Command(BaseCommand):
|
15 | 15 | |
16 | 16 | def handle(self, *args, **options): |
17 | 17 | example = options["example"] |
| 18 | if example == "raise": |
| 19 | raise CommandError() |
18 | 20 | self.stdout.write("I don't feel like dancing %s." % options["style"]) |
diff --git a/tests/modeltests/user_commands/tests.py b/tests/modeltests/user_commands/tests.py
index c1e2bf9..72674b1 100644
a
|
b
|
class CommandTests(TestCase):
|
26 | 26 | self.assertEqual(translation.get_language(), 'fr') |
27 | 27 | |
28 | 28 | def test_explode(self): |
| 29 | """ Test that an unknown command raises CommandError """ |
29 | 30 | self.assertRaises(CommandError, management.call_command, ('explode',)) |
| 31 | |
| 32 | def test_raiseonerror_arg(self): |
| 33 | """ Test that CommandError with call_command(raise_on_error=True) does |
| 34 | not produce a SystemExit exception. |
| 35 | """ |
| 36 | with self.assertRaises(CommandError): |
| 37 | management.call_command('dance', example="raise", raise_on_error=True) |