【发布时间】:2019-05-19 15:36:10
【问题描述】:
我不明白为什么 laravel 没有捕获 NotFoundHttpException,但它确实捕获了所有其他异常。
我想根据异常类型向我的用户发送自定义 json 响应,这适用于除 NotFoundHttpException 之外的所有列出的异常:
namespace App\Exceptions;
use Exception;
use Illuminate\Database\QueryException;
use Illuminate\Auth\AuthenticationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
public function render($request, Exception $exception)
{
switch (true) {
case $exception instanceof QueryException:
return response()->json('QueryException')
case $exception instanceof NotFoundHttpException:
return response()->json('NotFoundHttpException')
case $exception instanceof AuthenticationException:
return response()->json('AuthenticationException')
default:
return response()->json('Different Exception')
}
//return parent::render($request, $exception);
}
}
当我请求创建 NotFoundHttpException 时,laravel 会使用默认的 switch case 选项进行响应,对此有什么建议吗?
【问题讨论】:
-
尝试 instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
-
@AhmedAboud,我已经试过了,还是不行。