cloudcreativity / json-api

Framework agnostic JSON API serialisation and deserialisation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CloudCreativity\JsonApi\Error\ErrorObject toArray() enhancement?

nickelozz opened this issue · comments

Could it be possible to have the toArray() method (in CloudCreativity\JsonApi\Error\ErrorObject line 332) return only not null values?

I'll try to explain my scenario. I wanna be able to use this class to build application logic errors (as opposed to an Exception which handles "exceptional" situations) that I can then use to respond a client with using the following syntax:

$error = new ErrorObject([
    'status' => '418',
    'code' => '1',
    'title' => "I'm an error object",
    'detail' => 'error object',
    'meta' => ['render' => 'form'],
]);

return $this->reply()->respond('418', ['errors' => [$error->toArray()]]);

Currently if I do it like that, I get the following response:

{
  "errors": [
    {
      "id": null,
      "links": null,
      "status": "418",
      "code": "1",
      "title": "I'm an error object",
      "detail": "error object",
      "source": null,
      "meta": {
        "render": "form"
      }
    }
  ]
}

I'd rather not have all the null value keys returned from toArray(). Would implement such behavour break in some way the functionality of the class?

@nickelozz

Good point - I hadn't spotted this because I'm not using them in this way, but you're totally right that it shouldn't return null values.

I'll get this fixed.

@nickelozz

Have just realised this isn't a bug. If you're using the laravel-json-api package you don't need to reply with the errors - you need to the throw the error object so that the exception renderer renders them.

So your code should look like this:

$error = new ErrorObject([
    'status' => '418',
    'code' => '1',
    'title' => "I'm an error object",
    'detail' => 'error object',
    'meta' => ['render' => 'form'],
]);

throw new ErrorException($error);

When error objects are rendered, null values are ignored. That's because neomerx/json-api encodes objects that implement the ErrorInterface, which ErrorObject does.

Internally within ErrorObject, the reason toArray returns null values is because it is the opposite of exchangeArray - which accepts nulls. I hadn't really intended toArray to be used for encoding because neomerx/json-api handles the encoding of the interface.

Does that make sense? The thing I could add if it would be useful is to add a $this->reply()->errors($error) helper to the laravel package which would do the same rendering as if you threw the exception. Let me know if that would be useful as I'll happily add that.

Howdy @lindyhopchris!

Thanks for being so diligent about this... I guess I was trying not to use exception throwing for app logic errors but I'm not opposed to the idea of using the existing ErrorObject in combination with a helper method such as the one you mentioned (yes I know ultimately there'd be an exception throwing but I can live with it :P). $this->reply()->errors($error) would definetly be appreciated as I think it helps keep the controllers less cumbersome and it'd be overall more straightforward.

Please do add the helper!! :D

I really like both packages you've built to handle json-api and I expect to continue using them a lot in the future.

Closed as it's currently possible to go around the issue by using ErrorObject and ErrorException to create a valid JSON API response.

Great. Glad you like the packages and keep sending me an ideas/issues!

I've created an issue in the laravel-json-api package for the errors helper enhancement. I should be able to get it done some point in the next few days.