`Laravel`版`小丑路人社区`改版中,与`Hyperf版小丑路人社区`数据互动,此版本改版中……尚未彻底完结!

Q:

laravel的`App\Exceptions\Handler`异常类监听

文件 App\Exceptions\Handler

此项目由于仅API,所以全部json返回,可根据项目需要调整。

    public function render($request, Throwable $exception)
    {
        // 路由404异常监听
        if($exception instanceof NotFoundHttpException){
            $this->setHttpCode(404);
            return $this->errorJson("路由{{$request->path()}}不存在!");
        }

        if ($exception instanceof ModelNotFoundException){
            // var_dump('ModelNotFoundException');
            // var_dump($exception->getMessage());
        }

        // 验证器类的错误监听
        if($exception instanceof \Illuminate\Validation\ValidationException){
            return $this->errorJson($exception->validator->errors()->first());
        }

        // Exception类的错误监听
        if($exception instanceof \Exception){
            return $this->errorJson($exception->getMessage(), $exception->getCode(), [], [
                'file' => $exception->getFile(),
                'line' => $exception->getLine(),
            ]);
        }

        return parent::render($request, $exception);
    }
Laravel
订阅

评论记录


评论/回复