Code

Opened 3 years ago

Closed 2 years ago

#16397 closed Bug (fixed)

BaseCommand.execute() swallows ImportError, CommandError even when --traceback is used

Reported by: charles@… Owned by: aaugustin
Component: Core (Management commands) Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Any ImportError or CommandError happening during BaseCommand.execute() is emitted as a single-line exception, even if --traceback is used. This can make exceptions caused by import loops difficult to debug.

--traceback should be honored, emitting full stack traces.

Attachments (1)

16397.diff (1.7 KB) - added by aaugustin 3 years ago.

Download all attachments as: .zip

Change History (7)

comment:1 Changed 3 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Could you provide a snippet to help us reproduce this problem?

comment:2 Changed 3 years ago by anonymous

The easy-to-reproduce case is raising a CommandError from anywhere in execution:

from django.core.management.base import BaseCommand, CommandError

class Command(BaseCommand):
    """Trigger an ImportError with no stack trace even when --traceback is used"""

    help = 'Demonstrate #16397'

    def handle(self, *arguments, **options):
        raise CommandError('foo')

...in this case, running the command, even with --traceback, simply results in a single line of output: Error: foo.

However, this isn't much of a bug -- CommandErrors don't tend to happen anywhere but in management commands, and handling them in a user-friendly way tends to be desired behavior (though I think it might be fair to break that rule when --traceback is in use).

The much more interesting case is triggering an ImportError during translation.activate() -- this is what was biting me in practice. However, reproducing it (with the various bugs in my codebase which lead to it happening in practice fixed) appears to be nontrivial. I'll revisit this later.

comment:3 Changed 3 years ago by aaugustin

  • Has patch set
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to Bug

Changed 3 years ago by aaugustin

comment:4 Changed 3 years ago by adamnelson

  • Needs tests set

comment:5 Changed 2 years ago by aaugustin

  • Owner changed from nobody to aaugustin

#11667 was closed as a duplicate, check the discussions and patches over there before committing a fix for this ticket.

#17369 was also closed as a duplicate.

comment:6 Changed 2 years ago by aaugustin

  • Resolution set to fixed
  • Status changed from new to closed

In [17197]:

Fixed #16397 -- Respected the --traceback flag in BaseCommand. This should make import loops easier to debug. Refs #11667, #17369.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.