Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
At the present moment, an error response is normalized in the exception subscriber Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber
. However we want to allow other modules to choose how they serialize the exceptions to deliver errors to the consumers.
Proposed resolution
- Introduce HttpException normalizers for the core formats.
- Refactor the event subscriber to serialize the exception object directly. That way other formats can just provide the exception normalizers.
- Make sure the exception trace serializes correctly to put in the caches (PHP's serialize function). This is an issue if the exception object is added to the response data in ResourceResponse.
Remaining tasks
N/A
User interface changes
None.
API changes
None.
Data model changes
None.
Comments
Comment #2
e0ipsoThis is a similar effort that we had to undertake in jsonapi. #2810293: [FEATURE] Use exception event for exception response.
Comment #3
Grayside CreditAttribution: Grayside at Phase2 for Norwegian Cruise Line commentedComment #4
Wim LeersComment #5
Wim LeersIt's not clear to me what this is trying to achieve. When/why is this necessary?
But it definitely belongs in the
serialization.module
component (just likeDrupal\serialization\EventSubscriber\DefaultExceptionSubscriber
already is part of that), because the intent is for this to work for any implementation of web services, either "classical REST" (rest.module
) or for example JSON API or GraphQL.Comment #6
e0ipsoCore is doing inline exception normalization, which is only alterable by replacing the event subscriber. Instead, it should use the serialization system to deliver the information about the exception in the format it was negotiated.
See how http://cgit.drupalcode.org/drupal/tree/core/lib/Drupal/Core/EventSubscri... normalizes the exception as:
For JSON API (and most probably other formats) we want the ability to control the contents of the response to conform to a specification. We want something close to:
More examples of these inline normalizations are:
Comment #8
Wim LeersFYI: that specific code you linked to is being deleted in #2825347: 'Accept' header still is used for determining response format of error responses, ?_format= has no effect AND serialization module's exception subscriber does not support status code 500 error responses.
But that doesn't yet address your generic request: let a
Normalizer
generate the error response body forHttpException
s, instead of lettingKernelEvents::EXCEPTION
subscribers do that. Which is usually going to be\Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber::on4xx()
(since you wrote #6 and those links, we landed #2739617: Make it easier to write on4xx() exception subscribers, which made that a whole lot more consistent.Comment #9
Wim Leersd.o ate my issue title update :/
Comment #10
Wim LeersHowever, I do see one flaw in this proposal: #2838949: HttpException Handling Does Not Pass Headers to Responses — results e.g. in missing Allow header for 405 responses added the fix (and test coverage) to ensure that headers associated with an
HttpException
object actually do make it onto the error response too.If the exception is normalized into a response body, then how are we going to get that header to make it onto the error response?
Comment #11
Wim LeersAnd more importantly, I just discovered at #2829977-29: SerializableHttpException and Drupal\jsonapi\Error\* are not necessary that the only module and format that would benefit from this for now (JSON API) won't actually be able to benefit from this at all:
I do think the idea proposed in this issue is sound, but for now, nothing would be able to benefit from this. Therefore, marking this
for now. When the API-first module landscape in D8 is more mature, we should reconsider this!