laravel-jsonapi icon indicating copy to clipboard operation
laravel-jsonapi copied to clipboard

Returning error responses from customer handler

Open wireblue opened this issue 9 years ago • 3 comments

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

wireblue avatar Oct 29 '15 00:10 wireblue

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.

egeriis avatar Oct 29 '15 10:10 egeriis

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 avatar Oct 30 '15 04:10 wireblue

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.

wireblue avatar Dec 02 '15 04:12 wireblue