Handler.php 4.2 KB
<?php

namespace App\Exceptions;

use BadMethodCallException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Response;
use Symfony\Component\Debug\Exception\FatalThrowableError;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Throwable  $exception
     * @return void
     *
     * @throws \Throwable
     */
    public function report(Throwable $exception)
    {
//        // 捕获异常:路由不存在  No query results for model [App\\Models\
//        if ($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
//            //捕获路由模型绑定在数据库中找不到模型后抛出的NotFoundHttpException
//            return  Response::json(
//                [
//                    'code'  => 1001,
//                    'msg'   => '该路由不存在!',
//                    'data'  => null,
//                ]
//            );
//        }
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Throwable  $exception
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Throwable
     */
    public function render1($request, Throwable $exception)
    {
//        dump(1);
//        dd($exception);
        // 捕获异常  No query results for model [App\\Models\
//        if ($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
//            //捕获路由模型绑定在数据库中找不到模型后抛出的NotFoundHttpException
//            return  Response::json(
//                [
//                    'code' => 1,
//                    'msg' => '该数据不存在!',
//                    'data' => null,
//                ]
//            );
//        }

        // 捕获异常:路由不存在  No query results for model [App\\Models\
        if ($exception instanceof NotFoundHttpException) {
            //捕获路由模型绑定在数据库中找不到模型后抛出的NotFoundHttpException
            return  Response::json(
                [
                    'code'  => 1,
                    'msg'   => '该路由不存在!',
                    'data'  => null,
                ]
            );
        }

        // 捕获异常 BadMethodCallException
        if ($exception instanceof BadMethodCallException) {
            //捕获路由模型绑定在数据库中找不到模型后抛出的NotFoundHttpException
            return  Response::json(
                [
                    'code'  => 1,
                    'msg'   => $exception->getMessage(),
                    'data'  => null,
                ]
            );
        }

        // 捕获异常 FatalThrowableError
        if ($exception instanceof FatalThrowableError) {
            //捕获路由模型绑定在数据库中找不到模型后抛出的NotFoundHttpException
            return  Response::json(
                [
                    'code'  => 1,
                    'msg'   => $exception->getMessage(),
                    'data'  => null,
                ]
            );
        }

        // 捕获异常 MethodNotAllowedHttpException
        if ($exception instanceof MethodNotAllowedHttpException) {
            //捕获路由方法不对的问题
            return  Response::json(
                [
                    'code'  => 1,
                    'msg'   => $exception->getMessage(),
                    'data'  => null,
                ]
            );
        }

        // .. 继续补充其他异常

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