Roh*_*han 7 php exception-handling laravel laravel-5 jsend
我正在使用Laravel 5处理RESTful应用程序,我正在尝试捕获异常并生成适当的响应.我也在使用tymondesigns/jwt-auth包,所以所有的API响应都是JSend JSON格式.
现在我正试图抓住TokenExpiredException当给定令牌过期时出现的情况.所以我尝试了这个Handler.php:
if($e instanceof TokenExpiredException)
{
    return jsend()->error()
          ->message("Token Expired")
          ->code(403)
          ->data([null])
          ->get();
}
但我仍然无法捕获此异常并返回JSON响应.虽然我可以为其他例外执行此操作,例如:
if ($e instanceof ModelNotFoundException) {
    $e = new NotFoundHttpException($e->getMessage(), $e);
    return jsend()->error()
              ->message("404 Model Not Found")
              ->data([null])
              ->get();
}
和:
if ($this->isHttpException($e))
{       
    if($e instanceof NotFoundHttpException)
    {
        return jsend()->error()
              ->message("404 Route Not Found")
              ->data([null])
              ->get();
    }
    return $this->renderHttpException($e);
}
如何处理Laravel中的其他异常?
看来我忘记使用命名空间:
if($e instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException)
{
    return jsend()->error()
          ->message("Token Expired")
          ->code(403)
          ->data([null])
          ->get();
}
小错误!捂脸
| 归档时间: | 
 | 
| 查看次数: | 1134 次 | 
| 最近记录: |