I am working on a RESTful application using Laravel 5 and I am trying to catch exceptions and generate an appropriate response. I am also using the tymondesigns/jwt-auth package so that all the API responses are in JSend JSON format.
Right now I am trying to catch the TokenExpiredException
which arises when the given token is expired of course. So I tried this in the Handler.php
:
if($e instanceof TokenExpiredException)
{
return jsend()->error()
->message("Token Expired")
->code(403)
->data([null])
->get();
}
But I am still not able to catch this exception and give back a JSON response. Although I am able to do this for other exceptions like:
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
return jsend()->error()
->message("404 Model Not Found")
->data([null])
->get();
}
And:
if ($this->isHttpException($e))
{
if($e instanceof NotFoundHttpException)
{
return jsend()->error()
->message("404 Route Not Found")
->data([null])
->get();
}
return $this->renderHttpException($e);
}
How to handle other exceptions in Laravel?