Ticket #18387: 18387-1.diff

File 18387-1.diff, 3.2 KB (added by Claude Paroz, 8 years ago)

Add raise_on_error option

  • django/core/management/base.py

    diff --git a/django/core/management/base.py b/django/core/management/base.py
    index 6e06991..9031ce4 100644
    a b class BaseCommand(object): 
    266266                if self.output_transaction:
    267267                    self.stdout.write('\n' + self.style.SQL_KEYWORD("COMMIT;"))
    268268        except CommandError as e:
     269            if options.get('raise_on_error', False):
     270                raise
    269271            if show_traceback:
    270                 traceback.print_exc()
     272                self.stderr.write(traceback.format_exc())
    271273            else:
    272274                self.stderr.write('Error: %s' % e)
    273275            sys.exit(1)
  • docs/ref/django-admin.txt

    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``. 
    14681468``**options``
    14691469  named options accepted on the command-line.
    14701470
     1471.. versionadded:: 1.5
     1472
     1473If you pass ``raise_on_error=True`` as keyword argument to ``call_command``,
     1474the command will not call ``sys.exit(1)`` when a ``CommandError`` is raised, but
     1475will simply let ``CommandError`` propagate. This allows you to catch the
     1476exception, for example when you test a command.
     1477
    14711478Examples::
    14721479
    14731480      from django.core import management
     1481      from django.core.management.base import CommandError
     1482
    14741483      management.call_command('flush', verbosity=0, interactive=False)
    14751484      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          ...
  • tests/modeltests/user_commands/management/commands/dance.py

    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  
    11from optparse import make_option
    22
    3 from django.core.management.base import BaseCommand
     3from django.core.management.base import BaseCommand, CommandError
    44
    55
    66class Command(BaseCommand):
    class Command(BaseCommand): 
    1515
    1616    def handle(self, *args, **options):
    1717        example = options["example"]
     18        if example == "raise":
     19            raise CommandError()
    1820        self.stdout.write("I don't feel like dancing %s." % options["style"])
  • tests/modeltests/user_commands/tests.py

    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): 
    2626            self.assertEqual(translation.get_language(), 'fr')
    2727
    2828    def test_explode(self):
     29        """ Test that an unknown command raises CommandError """
    2930        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)
Back to Top