egeriis / laravel-jsonapi

Make it a breeze to create a jsonapi.org compliant APIs with Laravel 5.
MIT License
146 stars 27 forks source link

Returning error responses from customer handler #43

Open wireblue opened 9 years ago

wireblue commented 9 years ago

Hi all,

Not sure if this is incorrect usage or a bug, but I'm getting errors when returning error response from my customer handlers.

Example:

public function handleGet(ApiRequest $request)
{
    // ...
    return new ApiErrorResponse(404, 404, "Product doesn't exist.");
}

Because Handler.php is only looking for instances of "Response" (on Line 84), not ErrorResponse or MultiErrorResponse, it trips up when it tries to load the (non-existent) models.

I'd be happy to submit a simple PR to check for the other two response types, but I didn't know if this was the best way to go about it. Should ErrorResponse and MultiErrorResponse be inheriting from Response instead, or am I "doing it wrong"?

Thoughts?

Kyle

egeriis commented 9 years ago

Really depends on how your controller handles the Handler return value.

If you have a look at the example, you can see that JsonApi\Exceptions are caught and handled appropriately, but it's up to you to implement this logic.

wireblue commented 9 years ago

For the me the problem is occuring when I return an ErrorResponse object from my custom handler (eg. ProductsHandler.php) to this project's "Handler.php". This occurs within the "fulfillRequest" method. It hasn't yet returned back to the controller yet, so I'm unable to deal with it there.

Are you suggesting that I should implement my own version of "fulfillRequest", or bypass it completely in my controller?

wireblue commented 8 years ago

If it helps anyone else I ended up creating my own "BaseHandler" which extended the \EchoIt\JsonApi\Handler. I changed the "fulfillRequest" to suit my needs and updated my other handlers to extend from my new "BaseHandler".

I feel a fix to the \EchoIt\JsonApi\Handler would be more ideal, so I will leave this issue open and to the discretion of the repo maintainer.