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/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 | |