Skip to content

Ensure proper Exception inheritance in APIException subclasses#9883

Open
RinZ27 wants to merge 1 commit intoencode:mainfrom
RinZ27:fix-exception-inheritance
Open

Ensure proper Exception inheritance in APIException subclasses#9883
RinZ27 wants to merge 1 commit intoencode:mainfrom
RinZ27:fix-exception-inheritance

Conversation

@RinZ27
Copy link

@RinZ27 RinZ27 commented Feb 5, 2026

I noticed that APIException and ValidationError were overriding __init__ without calling super().__init__(). While DRF handles its own self.detail logic, skipping the base Exception.__init__ leaves exc.args empty. This can cause issues with error-tracking tools or logging middleware that expect standard exception attributes.

This change ensures that all DRF exceptions properly populate their base attributes while maintaining existing functionality. I've updated the initialization chain to ensure super() is called correctly throughout.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a gap in exception handling by ensuring that DRF's custom exceptions properly call their parent Exception.__init__() method. This fix ensures that standard exception attributes like args are properly populated, which is important for compatibility with error-tracking tools and logging middleware that rely on these standard attributes.

Changes:

  • Updated APIException.__init__() to call super().__init__() with the processed detail
  • Updated ValidationError.__init__() to call super().__init__() instead of directly setting self.detail

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@RinZ27 RinZ27 force-pushed the fix-exception-inheritance branch from 5a340d9 to 89020ae Compare February 5, 2026 12:24
detail = [detail]

self.detail = _get_error_details(detail, code)
super().__init__(self.detail, code)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im just curious, in what kind of scerio did this exception behavior affected you?

@RinZ27
Copy link
Author

RinZ27 commented Feb 6, 2026

@thawancomt It primarily affects interoperability with standard Python tooling. Since exc.args remains empty, any logging middleware or error-tracking services (like Sentry or custom OpenTelemetry exporters) that rely on inspecting args to extract the exception message or context will end up with missing data.

For example, a simple str(exc) on a standard Python exception normally returns its first arg, but here it might not behave as expected if the base class isn't initialized. By calling super().__init__(), we ensure DRF exceptions are "well-behaved" members of the Python exception hierarchy without breaking any existing DRF-specific logic.

@thawancomt
Copy link

I get it now, thx man

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.

2 participants