Code

Ticket #19923: 19923-2.diff

File 19923-2.diff, 3.5 KB (added by claudep, 14 months ago)

Same patch including updated docs

Line 
1diff --git a/django/core/management/base.py b/django/core/management/base.py
2index bdaa5fa..e6a968b 100644
3--- a/django/core/management/base.py
4+++ b/django/core/management/base.py
5@@ -241,7 +241,7 @@ class BaseCommand(object):
6         except Exception as e:
7             # self.stderr is not guaranteed to be set here
8             stderr = getattr(self, 'stderr', OutputWrapper(sys.stderr, self.style.ERROR))
9-            if options.traceback:
10+            if options.traceback or not isinstance(e, CommandError):
11                 stderr.write(traceback.format_exc())
12             else:
13                 stderr.write('%s: %s' % (e.__class__.__name__, e))
14diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
15index c2034a8..81877c7 100644
16--- a/docs/ref/django-admin.txt
17+++ b/docs/ref/django-admin.txt
18@@ -1342,8 +1342,9 @@ Example usage::
19     django-admin.py syncdb --traceback
20 
21 By default, ``django-admin.py`` will show a simple error message whenever an
22-error occurs. If you specify ``--traceback``, ``django-admin.py``  will
23-output a full stack trace whenever an exception is raised.
24+:class:`~django.core.management.CommandError` occurs, but a full stack trace
25+for any other exception. If you specify ``--traceback``, ``django-admin.py``
26+will also output a full stack trace when a ``CommandError`` is raised.
27 
28 .. django-admin-option:: --verbosity
29 
30diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py
31index 4259598..90f7720 100644
32--- a/tests/admin_scripts/tests.py
33+++ b/tests/admin_scripts/tests.py
34@@ -16,7 +16,7 @@ import codecs
35 
36 from django import conf, bin, get_version
37 from django.conf import settings
38-from django.core.management import BaseCommand
39+from django.core.management import BaseCommand, CommandError
40 from django.db import connection
41 from django.test.simple import DjangoTestSuiteRunner
42 from django.utils import unittest
43@@ -1297,22 +1297,34 @@ class CommandTypes(AdminScriptTestCase):
44         Also test proper traceback display.
45         """
46         command = BaseCommand()
47-        command.execute = lambda args: args  # This will trigger TypeError
48+        def raise_command_error(*args, **kwargs):
49+            raise CommandError("Custom error")
50 
51         old_stderr = sys.stderr
52         sys.stderr = err = StringIO()
53         try:
54+            command.execute = lambda args: args  # This will trigger TypeError
55             with self.assertRaises(SystemExit):
56                 command.run_from_argv(['', ''])
57             err_message = err.getvalue()
58-            self.assertNotIn("Traceback", err_message)
59+            # Exceptions other than CommandError automatically output the traceback
60+            self.assertIn("Traceback", err_message)
61             self.assertIn("TypeError", err_message)
62 
63+            command.execute = raise_command_error
64+            err.truncate(0)
65+            with self.assertRaises(SystemExit):
66+                command.run_from_argv(['', ''])
67+            err_message = err.getvalue()
68+            self.assertNotIn("Traceback", err_message)
69+            self.assertIn("CommandError", err_message)
70+
71+            err.truncate(0)
72             with self.assertRaises(SystemExit):
73                 command.run_from_argv(['', '', '--traceback'])
74             err_message = err.getvalue()
75             self.assertIn("Traceback (most recent call last)", err_message)
76-            self.assertIn("TypeError", err_message)
77+            self.assertIn("CommandError", err_message)
78         finally:
79             sys.stderr = old_stderr
80