diff --git a/django/core/management/base.py b/django/core/management/base.py
index bdaa5fa..e6a968b 100644
|
a
|
b
|
class BaseCommand(object):
|
| 241 | 241 | except Exception as e: |
| 242 | 242 | # self.stderr is not guaranteed to be set here |
| 243 | 243 | stderr = getattr(self, 'stderr', OutputWrapper(sys.stderr, self.style.ERROR)) |
| 244 | | if options.traceback: |
| | 244 | if options.traceback or not isinstance(e, CommandError): |
| 245 | 245 | stderr.write(traceback.format_exc()) |
| 246 | 246 | else: |
| 247 | 247 | stderr.write('%s: %s' % (e.__class__.__name__, e)) |
diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
index c2034a8..81877c7 100644
|
a
|
b
|
Example usage::
|
| 1342 | 1342 | django-admin.py syncdb --traceback |
| 1343 | 1343 | |
| 1344 | 1344 | By default, ``django-admin.py`` will show a simple error message whenever an |
| 1345 | | error occurs. If you specify ``--traceback``, ``django-admin.py`` will |
| 1346 | | output a full stack trace whenever an exception is raised. |
| | 1345 | :class:`~django.core.management.CommandError` occurs, but a full stack trace |
| | 1346 | for any other exception. If you specify ``--traceback``, ``django-admin.py`` |
| | 1347 | will also output a full stack trace when a ``CommandError`` is raised. |
| 1347 | 1348 | |
| 1348 | 1349 | .. django-admin-option:: --verbosity |
| 1349 | 1350 | |
diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py
index 4259598..90f7720 100644
|
a
|
b
|
import codecs
|
| 16 | 16 | |
| 17 | 17 | from django import conf, bin, get_version |
| 18 | 18 | from django.conf import settings |
| 19 | | from django.core.management import BaseCommand |
| | 19 | from django.core.management import BaseCommand, CommandError |
| 20 | 20 | from django.db import connection |
| 21 | 21 | from django.test.simple import DjangoTestSuiteRunner |
| 22 | 22 | from django.utils import unittest |
| … |
… |
class CommandTypes(AdminScriptTestCase):
|
| 1297 | 1297 | Also test proper traceback display. |
| 1298 | 1298 | """ |
| 1299 | 1299 | command = BaseCommand() |
| 1300 | | command.execute = lambda args: args # This will trigger TypeError |
| | 1300 | def raise_command_error(*args, **kwargs): |
| | 1301 | raise CommandError("Custom error") |
| 1301 | 1302 | |
| 1302 | 1303 | old_stderr = sys.stderr |
| 1303 | 1304 | sys.stderr = err = StringIO() |
| 1304 | 1305 | try: |
| | 1306 | command.execute = lambda args: args # This will trigger TypeError |
| 1305 | 1307 | with self.assertRaises(SystemExit): |
| 1306 | 1308 | command.run_from_argv(['', '']) |
| 1307 | 1309 | err_message = err.getvalue() |
| 1308 | | self.assertNotIn("Traceback", err_message) |
| | 1310 | # Exceptions other than CommandError automatically output the traceback |
| | 1311 | self.assertIn("Traceback", err_message) |
| 1309 | 1312 | self.assertIn("TypeError", err_message) |
| 1310 | 1313 | |
| | 1314 | command.execute = raise_command_error |
| | 1315 | err.truncate(0) |
| | 1316 | with self.assertRaises(SystemExit): |
| | 1317 | command.run_from_argv(['', '']) |
| | 1318 | err_message = err.getvalue() |
| | 1319 | self.assertNotIn("Traceback", err_message) |
| | 1320 | self.assertIn("CommandError", err_message) |
| | 1321 | |
| | 1322 | err.truncate(0) |
| 1311 | 1323 | with self.assertRaises(SystemExit): |
| 1312 | 1324 | command.run_from_argv(['', '', '--traceback']) |
| 1313 | 1325 | err_message = err.getvalue() |
| 1314 | 1326 | self.assertIn("Traceback (most recent call last)", err_message) |
| 1315 | | self.assertIn("TypeError", err_message) |
| | 1327 | self.assertIn("CommandError", err_message) |
| 1316 | 1328 | finally: |
| 1317 | 1329 | sys.stderr = old_stderr |
| 1318 | 1330 | |