Skip to content

ClickExceptions with no ctx cause an error #29

@dchiquito

Description

@dchiquito

Doing something as simple as raise ClickException('my error message') results in an error being thrown during the handling of the ClickException:

Traceback (most recent call last):
  File "/home/daniel.chiquito/envs/dandi-api/lib/python3.8/site-packages/djclick/adapter.py", line 69, in run_from_argv
    exit_code = self.main(
  File "/home/daniel.chiquito/envs/dandi-api/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/daniel.chiquito/envs/dandi-api/lib/python3.8/site-packages/djclick/adapter.py", line 51, in invoke
    return super(DjangoCommandMixin, self).invoke(ctx)
  File "/home/daniel.chiquito/envs/dandi-api/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/daniel.chiquito/envs/dandi-api/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/daniel.chiquito/git/dandi-api/dandiapi/api/management/commands/collect_garbage.py", line 26, in collect_garbage
    raise click.ClickException('my error message')
click.exceptions.ClickException: my error message

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./manage.py", line 20, in <module>
    main()
  File "./manage.py", line 16, in main
    execute_from_command_line(sys.argv)
  File "/home/daniel.chiquito/envs/dandi-api/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/home/daniel.chiquito/envs/dandi-api/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/daniel.chiquito/envs/dandi-api/lib/python3.8/site-packages/djclick/adapter.py", line 76, in run_from_argv
    if getattr(e.ctx, "traceback", False):  # NOCOV
AttributeError: 'ClickException' object has no attribute 'ctx'

The error is occurring here:
https://github.com/GaretJax/django-click/blob/73be8fb2948c0992fcf88644039cd26be84ebf12/djclick/adapter.py#L73-L77
The problem is that many subclasses of ClickException include a ctx, but not all: https://github.com/pallets/click/blob/9da166957f5848b641231d485467f6140bca2bc0/src/click/exceptions.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions