What were the reasons or design decisions behind wrapping APIExceptions in an ErrorDetail object? #8172
Replies: 1 comment
-
I'm also trying to customize DRF's error schema to improve DX for my colleagues working in frontend so they can benefit that using it with TypeScript. Right now, I'm trying to achieve something simple like this below: Note:
Even though I override I believe the lack of examples on this matter deserves serious attention. |
Beta Was this translation helpful? Give feedback.
-
I am trying to understand the way DRF works internally to enable myself to extend the functionality to fit my specific needs. I highly appreciate the time you have taken to help me understand the design philosophy/choices behind DRF.
A very common scenario I keep coming across in many questions is users wanting to add more details to the error responses produced by DRF app. As described in the documentation, we can alter the style of the response, by adding a custom exception handler. However, speaking from a development standpoint, IMHO, it is much easier to add additional contextual information while raising the error and then simply serialize it into a desired representation rather than add the information after it is raised.
However, it seems like adding additional contextual information while raising the error is not a simple task if you subclass
APIException
, as it coerces the details into anErrorDetail
object. Why was this designed this way? If it was just using some dictionary like structure, it would have been much easier to subclass theAPIException
classes no?We can get around all
APIExceptions
by defining our own exception classes derived fromException
and handling it incustom_exception_handler
, except forValidationErrors
as this is raised in theSerializer
,Field
andValidator
classes. One way I have found to get around this is to override theis_valid
method and catch theserializers.ValidationError
and raise my ownCustomValidationError
derived fromException
and include any additional fields as part of the initialization process and then handle it in thecustom_exception_handler
.Thanks
Beta Was this translation helpful? Give feedback.
All reactions