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
We will want to implement the expected error format by JSON API. See http://jsonapi.org/format/upcoming/#errors
Proposed resolution
We should catch the exceptions and serialize the response. See for instance the deserializeBody
method. We'll want to do something similar to that. Obviously we still need to have the serializer act on the exception class and create the appropiate output format.
try {
return $serializer->deserialize($received, DocumentWrapperInterface::class, $format, array('request_method' => $method));
}
catch (UnexpectedValueException $e) {
$content = $serializer->serialize($e, $format);
return new Response($content, 400, array('Content-Type' => $request->getMimeType($format)));
}
Comment | File | Size | Author |
---|---|---|---|
#15 | 2738269--error-exceptions--15.patch | 10.48 KB | e0ipso |
#14 | 2738269--error-exceptions--14.patch | 7.43 KB | e0ipso |
Comments
Comment #2
gabesulliceWorking on this today. Adding errors to the document is similar enough to adding links that it makes sense to do this to unblock pagination.
Comment #3
e0ipsoStealing this from @gabesullice. I am pretty sure that he was working on #2742769: [FEATURE] Add support for sort.
Comment #4
dawehnerI'll give it a try
Comment #5
e0ipsoMy thoughts on this were something along the lines of a Normalizer for
HttpException
. So we can catch and serialize in theRequestHandler
. But this are just initial thoughts & brainstorming.Comment #6
dawehnerI rather enjoyed the weekend, so I should not try to promise anything, but I have a bit of a flight in front of me.
Having a dedicated serialization for exceptions could be really valuable. Let's hope the exceptions are semantic enough that we can actually extract the information out from there.
Comment #7
dawehnerJust an idea I had in the meantime. Allow exceptions to define all those properties on itself
Comment #8
e0ipsoMaybe we want to leverage the extra info already compilated in https://github.com/RESTful-Drupal/restful/blob/7.x-2.x/src/Exception/Res...
Comment #9
dawehnerThat could be absolutely a helpful exception class. So yeah my plan was kinda to throw exceptions as we go and then convert them and leverage it in the HTTP response. Do you think that makes sense in general?
Comment #10
gabesulliceNice!
Should this be JsonApiExceptionInterface? Capital "A".
Perhaps JsonApiException should simply implement NormalizerInterface so we don't need all this implementation detail coded into RequestHandler?
Comment #11
gabesulliceComment #12
dawehner@gabesullice
That is a good idea.
This is just continues work, nowhere needs perfect/comittable.
Comment #13
e0ipsoworking on this.
Comment #14
e0ipsoFeature implemented and tests added.
I added a bit of extra information if the user can see the reports.
Comment #15
e0ipsoThis will process PHP errors and warnings. I created a follow up to add the notices and warnings to the normal payload in the response, see #2759811: [FEATURE] Add notices and warnings to the normal response.
Comment #17
dawehnerIMHO having the more verbose interface would be valueable, but sure, we can do that later.
Comment #18
e0ipso@dawehner Agreee. I only reverted it because it would require to track the existing HttpExceptions and have them decorated with JsonApiException. My intention was to add a follow up, but I forgot. :-P
Comment #19
dawehnerHa, well we can keep this issue or move along to yet another issue :)
Comment #20
e0ipsoComment #21
e0ipsoFollow up created.
Comment #22
e0ipso