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)));
    }
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

e0ipso created an issue. See original summary.

gabesullice’s picture

Assigned: Unassigned » gabesullice

Working on this today. Adding errors to the document is similar enough to adding links that it makes sense to do this to unblock pagination.

e0ipso’s picture

Assigned: gabesullice » Unassigned

Stealing this from @gabesullice. I am pretty sure that he was working on #2742769: [FEATURE] Add support for sort.

dawehner’s picture

Assigned: Unassigned » dawehner

I'll give it a try

e0ipso’s picture

My thoughts on this were something along the lines of a Normalizer for HttpException. So we can catch and serialize in the RequestHandler. But this are just initial thoughts & brainstorming.

dawehner’s picture

Assigned: dawehner » Unassigned

I 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.

dawehner’s picture

Status: Active » Needs review
FileSize
4.51 KB

Just an idea I had in the meantime. Allow exceptions to define all those properties on itself

e0ipso’s picture

Maybe we want to leverage the extra info already compilated in https://github.com/RESTful-Drupal/restful/blob/7.x-2.x/src/Exception/Res...

dawehner’s picture

That 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?

gabesullice’s picture

  1. +++ b/src/Exception/JsonApiExceptionTrait.php
    @@ -0,0 +1,142 @@
    +trait JsonApiExceptionTrait {
    

    Nice!

  2. +++ b/src/Exception/JsonapiExceptionInterface.php
    @@ -0,0 +1,21 @@
    +interface JsonapiExceptionInterface {
    

    Should this be JsonApiExceptionInterface? Capital "A".

  3. +++ b/src/RequestHandler.php
    @@ -73,7 +74,35 @@ class RequestHandler extends RestRequestHandler {
    -      $error['error'] = $e->getMessage();
    +      $error['error'] = [
    +        'detail' => $e->getMessage(),
    +        'status' => $e->getStatusCode(),
    +      ];
    +
    +      if ($e instanceof JsonapiExceptionInterface) {
    +        if ($e->getId()) {
    +          $error['error']['id'] = $e->getId();
    +        }
    +        if ($e->getLinks()) {
    +          $error['error']['links'] = $e->getLinks();
    +        }
    +        if ($e->getCode()) {
    +          $error['error']['code'] = $e->getCode();
    +        }
    +        if ($e->getTitle()) {
    +          $error['error']['title'] = $e->getTitle();
    +        }
    +        if ($e->getDetail()) {
    +          $error['error']['detail'] = $e->getDetail();
    +        }
    +        if ($e->getSource()) {
    +          $error['error']['source'] = $e->getSource();
    +        }
    +        if ($e->getMeta()) {
    +          $error['error']['meta'] = $e->getMeta();
    +        }
    +      }
    +
           $content = $serializer->serialize($error, $format);
    

    Perhaps JsonApiException should simply implement NormalizerInterface so we don't need all this implementation detail coded into RequestHandler?

gabesullice’s picture

Status: Needs review » Needs work
dawehner’s picture

Status: Needs work » Needs review
FileSize
5.65 KB
0 bytes

@gabesullice
That is a good idea.

This is just continues work, nowhere needs perfect/comittable.

e0ipso’s picture

Assigned: Unassigned » e0ipso

working on this.

e0ipso’s picture

Feature implemented and tests added.

I added a bit of extra information if the user can see the reports.

e0ipso’s picture

This 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.

  • dawehner committed 2586eb4 on 8.x-1.x authored by e0ipso
    Issue #2738269 by dawehner, e0ipso: [FEATURE] Implement expected error...
dawehner’s picture

Status: Needs review » Active

IMHO having the more verbose interface would be valueable, but sure, we can do that later.

e0ipso’s picture

@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

dawehner’s picture

My intention was to add a follow up, but I forgot. :-P

Ha, well we can keep this issue or move along to yet another issue :)

e0ipso’s picture

Status: Active » Fixed
e0ipso’s picture

Follow up created.

e0ipso’s picture

Issue tags: +Dublin2016

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.