Skip to content

Use Strawberry exception handlers for Django errors - #935

Draft
patrick91 wants to merge 1 commit into
mainfrom
2026-07-13-use-strawberry-exception-handlers-for-django-error
Draft

Use Strawberry exception handlers for Django errors#935
patrick91 wants to merge 1 commit into
mainfrom
2026-07-13-use-strawberry-exception-handlers-for-django-error

Conversation

@patrick91

@patrick91 patrick91 commented Jul 13, 2026

Copy link
Copy Markdown
Member

Stack 🍰

Summary by Sourcery

Introduce a reusable Django exception handler that converts common Django errors into OperationInfo and integrate it into mutation error handling, while updating schema extension usage and dependencies.

New Features:

  • Add a DjangoExceptionHandler schema-level handler to convert ValidationError, PermissionDenied, and ObjectDoesNotExist into OperationInfo results.
  • Expose DjangoExceptionHandler from the main strawberry_django package for convenient import.

Enhancements:

  • Refactor Django mutation error handling to reuse the shared OperationInfo conversion logic and narrow caught exceptions.
  • Update tests and schema definitions to use extension classes/factories instead of instantiated extensions where required.
  • Document how to enable DjangoExceptionHandler for handling errors outside mutation resolvers.
  • Declare compatibility with a newer strawberry-graphql version and update the dependency constraint.

Documentation:

  • Add guidance on configuring DjangoExceptionHandler to handle Django errors raised outside mutation resolvers, including during input construction and field extensions.

Tests:

  • Add comprehensive tests covering DjangoExceptionHandler behavior, including argument conversion errors, sync and async field extensions, concurrency safety, and fallback to normal GraphQL errors when OperationInfo is not in the return union.

Chores:

  • Add a release note describing the new DjangoExceptionHandler feature and its integration with handle_django_errors.

@botberry

Copy link
Copy Markdown
Member

Thanks for adding the RELEASE.md file!

Below is the changelog that will be used for the release.


Add DjangoExceptionHandler, a schema-level Strawberry exception handler for
converting Django ValidationError, PermissionDenied, and ObjectDoesNotExist
exceptions into OperationInfo results. Combined with handle_django_errors=True,
it also handles exceptions raised during input conversion or by sync and async field
extensions.

This release was contributed by @patrick91 in #935

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 97.50000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 91.68%. Comparing base (17db928) to head (0a4e6a6).

Files with missing lines Patch % Lines
strawberry_django/exception_handlers.py 96.96% 1 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #935      +/-   ##
==========================================
+ Coverage   91.49%   91.68%   +0.18%     
==========================================
  Files          50       51       +1     
  Lines        4646     4654       +8     
==========================================
+ Hits         4251     4267      +16     
+ Misses        395      387       -8     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants