Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use Django management CommandError to eliminate the traceback on error #693

Merged
merged 1 commit into from
Mar 27, 2022

Conversation

quodlibetor
Copy link
Contributor

Instead of the long traceback, the error now looks like:

Schema generation summary:
Warnings: 4 (1 unique)
Errors:   0 (0 unique)

SchemaValidationError: Failing as requested due to warnings

Instead of the long traceback, the error now looks like:

    Schema generation summary:
    Warnings: 4 (1 unique)
    Errors:   0 (0 unique)

    SchemaValidationError: Failing as requested due to warnings
@tfranzel
Copy link
Owner

Hi @quodlibetor and thanks for the contribution. Not sure why mypy/linting is throwing a hissy fit but lets talk about the content first.

So what exactly is the motivation here? Is it just the validation traceback or any traceback. If it is about the validation, I mean removing the traceback takes away your ability to understand why exactly the schema is supposedly invalid. Is it worth it since under normal conditions no warning/validation errors should happen anyway and while developing you want that context. Am I missing something?

@quodlibetor
Copy link
Contributor Author

(The error is because django-stubs updated but drf-stubs hasn't yet: typeddjango/djangorestframework-stubs#203)

The TB I'm suppressing doesn't explain why the schema is invalid, it just shows where the error message was printed.

Here is an example current traceback, note that it points just to the file that I edited, not to anything in validation:

Schema generation summary:
Warnings: 4 (1 unique)
Errors:   0 (0 unique)

Traceback (most recent call last):
  File ".../manage.py", line 22, in <module>
    main()
  File ".../manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File ".../.venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File ".../.venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ".../.venv/lib/python3.10/site-packages/django/core/management/base.py", line 414, in run_from_argv
    self.execute(*args, **cmd_options)
  File ".../.venv/lib/python3.10/site-packages/django/core/management/base.py", line 460, in execute
    output = self.handle(*args, **options)
  File ".../.venv/lib/python3.10/site-packages/drf_spectacular/management/commands/spectacular.py", line 55, in handle
    raise RuntimeError('Failing as requested due to warnings')
RuntimeError: Failing as requested due to warnings

The new error message contains all the existing useful information:

Schema generation summary:
Warnings: 4 (1 unique)
Errors:   0 (0 unique)

SchemaValidationError: Failing as requested due to warnings

@tfranzel tfranzel merged commit a57ce64 into tfranzel:master Mar 27, 2022
@tfranzel
Copy link
Owner

(The error is because django-stubs updated but drf-stubs hasn't yet: typeddjango/djangorestframework-stubs#203)

yeah thanks, I already pinned the dep.

Now I get what you meant. I was at the validation step mentally. Totally agree! Just had to make sure the CLI still has a returncode != 0 to not break toolings.

I fixed the failing test, renamed the error to SchemaGenerationError so no confusion happens with --validation, but since this is looking a lot nicer I also wrapped the actual validation in SchemaValidationError.

Thank you.

@quodlibetor quodlibetor deleted the nicer-error branch March 28, 2022 00:07
@quodlibetor
Copy link
Contributor Author

nice, thanks!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants