I'm using repositories to share logic between the API and the application, when I query for a model and it doesn't exist it uses findOrFail which throws a Illuminate\Database\Eloquent\ModelNotFoundException. The problem is, at its core this extension extends the core's RuntimeException instead of a Symfony HTTP Exception so it's not caught by the API's exception handler. How would I go about to catch it globally without wrapping every single one of my methods in a try catch ?
I saw an API::error method but I'm unsure as to how it's meant to be used, would that be it ?
I'm being cautious about what exceptions the API catches only because it
would probably be a bit more involved for exceptions with no related status
code. I'm traveling at the moment so not near a computer but will think on
this. If you think of something let me know.
On 14 May 2014 18:16, "Maxime Fabre" [email protected] wrote:
I'm using repositories to share logic between the API and the application,
when I query for a model and it doesn't exist it uses findOrFail which
throws a IlluminateDatabaseEloquentModelNotFoundException. The problem
is, at its core this extension extends the core's RuntimeExceptioninstead of a Symfony HTTP Exception so it's not caught by the API's
exception handler. How would I go about to catch it globally without
wrapping every single one of my methods in a try catch ?—
Reply to this email directly or view it on GitHubhttps://github.com/dingo/api/issues/45
.
Well in this particular case it should obviously throw a 404, but I'm not asking it to be core (which would be nice), just if there's a way to append it manually to the list of caught exceptions. I tried this, reasoning that API::error worked like App::error but that didn't seem to do much:
API::error(function(Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return Response::json(['error' => 'nope']);
});
OK got why, the catch of API's router doesn't handle non-HTTP exceptions:
try
{
$response = parent::dispatch($request);
}
catch (HttpExceptionInterface $exception)
Shouldn't it though? I mean since you manually register handlers anyway, if a non-HTTP Exception is thrown it'll just be rethrown unless the user manually registers an API handler for it no ?
Nah the API doesn't even look at it. But that could be easy enough to do.
If there's no handler it'll just rethrow it.
On 14 May 2014 18:47, "Maxime Fabre" [email protected] wrote:
Well in this particular case it should obviously throw a 404, but I'm not
asking it to be core (which would be nice), just if there's a way to append
it manually to the list of caught exceptions. I tried this, reasoning that
API::error worked like App::error but that didn't seem to do much:API::error(function(ModelNotFoundException $e) {
return Response::json(['error' => 'nope']);});—
Reply to this email directly or view it on GitHubhttps://github.com/dingo/api/issues/45#issuecomment-43058986
.
Yeah I think its safe to assume that a ModelNotFound is a 404. Maybe not for every L4 app ever, but in the instance of an API it certainly would be.
If people want to catch it and do something specific then they can do that in their controller, right?
Yeah but I don't have one controller with one method, I have a large API, I would have liked a way to catch it globally without 43 identical try/catch, like App::error offers.
If you reread what I said, I was suggesting that ModelNotFound = 404 by default, with the option to override. I am agreeing with you. :)
Yeah no I know, I mean for other exceptions when you say this can be done at the controller level, technically it can, it's just, not easy :p
I think the idea would be to simply check if there's an exception handler
registered for the API and then let it handle it otherwise re-throw. I'm
not terribly keen on letting the API handle non-HTTP exceptions by default.
On 14 May 2014 23:43, "Maxime Fabre" [email protected] wrote:
Yeah no I know, I mean for other exceptions when you say this can be done
at the controller level, technically it can, it's just, not easy :p—
Reply to this email directly or view it on GitHubhttps://github.com/dingo/api/issues/45#issuecomment-43085280
.
That would be nice. For now I'm using a method every time I need to call firstOrFail()
protected function handleNotFoundException(Closure $callback)
{
try
{
return $callback();
}
catch(ModelNotFoundException $e)
{
return App::abort(404);
}
}
You should be able to write custom handlers for non-HTTP exceptions now.
API::error(function(Illuminate\Database\Eloquent\ModelNotFoundException $exception)
{
return Response::create('Model not found.', 404);
});
@jasonlewis where should we put this code?
@faridmovsumov: you can create your own service provider like ExceptionServiceProvider and put this snippet into the boot method.
Most helpful comment
@faridmovsumov: you can create your own service provider like
ExceptionServiceProviderand put this snippet into thebootmethod.