Reputation: 3538
I am using Laravel 5.3. I have few APIs where user will request for specific id.
For example url to subscribe an event
example.com/api/event/{id}/subscribe
Normally if id
not exist, Laravel will return response 500 with error message "Trying to get property of non-object"
So I would add check if event is exist like below in every controller where model 'id' id passed:
$event = Event::find($id)
if ($event) {
// return json data
}
else {
return response()->json([
'status' => 'object not found'
], 404);
}
My question is, any better solution to handle this globally to check if object requested is not exist? My current solution is here, but I might think there should be better one
I add this code into my app/Exception/Handler.php
, so every api request non-existing object will return 404 with specific json message. So the API consumer will know the object id not valid.
public function render($request, Exception $exception)
{
// global exception handler if api request for non existing object id
if ($request->wantsJson() && $exception->getMessage() == 'Trying to get property of non-object') {
return response()->json([
'status' => 'object requested not found'
], 404);
}
return parent::render($request, $exception);
}
Thanks in advance!
Upvotes: 5
Views: 7111
Reputation: 17658
You can use render()
function of App\Exceptions\Handler
class as:
public function render($request, Exception $exception)
{
if ($request->wantsJson() && $exception instanceof ModelNotFoundException) {
return response()->json(['status' => 'object requested not found'], 404);
}
return parent::render($request, $exception);
}
And remember to add following code:
use Illuminate\Database\Eloquent\ModelNotFoundException;
Upvotes: 10
Reputation: 1582
Try changing your
$event = Event::find($id)
to
$event = Event::findOrFail($id)
As far I remember, it'll throw a ModelNotFoundException
if it couldn't find anything for that id. Go to app/Exceptions/Handler.php
and inside the render method, catch the exception and handle it.
Edited:
if ($e instanceof HttpResponseException) {
return $e->getResponse();
} elseif ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
} elseif ($e instanceof AuthenticationException) {
return $this->unauthenticated($request, $e);
} elseif ($e instanceof AuthorizationException) {
$e = new HttpException(403, $e->getMessage());
} elseif ($e instanceof ValidationException && $e->getResponse()) {
return $e->getResponse();
}
You can see that the parent render method fires a NotFoundHttpException
if it gets a ModelNotFoundException
exception. I guess you can overwrite it to match your requirement.
Upvotes: 4