Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nickelozz opened this issue Oct 15, 2015 · 4 comments
Closed

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

nickelozz opened this issue Oct 15, 2015 · 4 comments

Comments

@nickelozz
Copy link

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?

@lindyhopchris
Copy link
Member

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

@lindyhopchris
Copy link
Member

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

@nickelozz
Copy link
Author

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.

@lindyhopchris
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants