Ensure proper Exception inheritance in APIException subclasses#9883
Ensure proper Exception inheritance in APIException subclasses#9883RinZ27 wants to merge 1 commit intoencode:mainfrom
Conversation
There was a problem hiding this comment.
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 callsuper().__init__()with the processed detail - Updated
ValidationError.__init__()to callsuper().__init__()instead of directly settingself.detail
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
5a340d9 to
89020ae
Compare
| detail = [detail] | ||
|
|
||
| self.detail = _get_error_details(detail, code) | ||
| super().__init__(self.detail, code) |
There was a problem hiding this comment.
Im just curious, in what kind of scerio did this exception behavior affected you?
|
@thawancomt It primarily affects interoperability with standard Python tooling. Since For example, a simple |
|
I get it now, thx man |
I noticed that
APIExceptionandValidationErrorwere overriding__init__without callingsuper().__init__(). While DRF handles its ownself.detaillogic, skipping the baseException.__init__leavesexc.argsempty. 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.