Handler.php 4.8 KB
<?php

namespace App\Exceptions;

use App\Enums\Common\Code;
use App\Services\DingService;
use App\Utils\EncryptUtils;
use App\Utils\LogUtils;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;

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

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

    /**
     * Register the exception handling callbacks for the application.
     *
     * @return void
     */
    public function register()
    {
        $this->reportable(function (Throwable $e) {
            //
        });
    }
    /**
     * Report or log an exception.
     *
     * @param \Throwable $exception
     * @return void
     *
     * @throws \Throwable
     */
    public function report(Throwable $exception)
    {
        //日志记录
        $exceptionMessage = "错误CODE:" . $exception->getCode() .
            "-----错误message:" . $exception->getMessage() .
            '------错误文件:' . $exception->getFile() .
            '-------错误行数:' . $exception->getLine();
        //A端错误
        if ($exception instanceof AsideGlobalException) {
            LogUtils::error("AsideGlobalException", [], $exceptionMessage);
        }
        //B端错误
        elseif($exception instanceof BsideGlobalException) {
            LogUtils::error("BsideGlobalException", [], $exceptionMessage);
        }
        //C端错误
        elseif($exception instanceof CsideGlobalException) {
            LogUtils::error("CsideGlobalException", [], $exceptionMessage);
        }
        //验证错误(非手动抛出)
        elseif ($exception instanceof ValidationException) {
            LogUtils::error("参数验证失败", [], $exceptionMessage);
        }
        //Redis错误(非手动抛出)
        elseif ($exception instanceof \RedisException) {
            LogUtils::error("Redis服务异常", [], $exceptionMessage);
        }
        //Mysql错误(非手动抛出)
        elseif ($exception instanceof \PDOException) {
            LogUtils::error("数据库服务异常", [], $exceptionMessage);
        }
        //模型未找到错误(非手动抛出)
        elseif ($exception instanceof ModelNotFoundException) {
            LogUtils::error("模型资源未找到", [], $exceptionMessage);
        }
        //其他
        else {
            LogUtils::error("全局系统异常", [], $exceptionMessage);
        }
    }
    /**
     * Render an exception into an HTTP response.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Throwable $exception
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Throwable
     */
    public function render($request, Throwable $exception)
    {
        $message = $exception->getMessage();

        if ($exception instanceof AsideGlobalException) {
            $code = $exception->getCode();
        }elseif ($exception instanceof BsideGlobalException) {
            $code = $exception->getCode();
        }elseif ($exception instanceof CsideGlobalException) {
            $code = $exception->getCode();
        } elseif ($exception instanceof ValidationException) {
            $code = Code::USER_PARAMS_ERROE();
            $message = $code->description = Arr::first(Arr::first($exception->errors()));
        } elseif ($exception instanceof NotFoundHttpException || $exception instanceof MethodNotAllowedHttpException) {
            return response('404 Not Found', 404);
        } else {
            $code = Code::SYSTEM_ERROR();
        }
        //开启debug 错误原样输出
        $debub = config('app.debug');
        $message = $debub ? $message : ($code->description ?? $message);
        $response = [
            'code' => $code,
            'message' => $message
        ];
        // 调试模式
        if(env('app_debug')){
            $response['trace']   =   $exception->getTrace();
        }

        //加密返回
        if (config('app.params_encrypt')) {
            $k = config('app.params_encrypt_key');
            $i = config('app.params_encrypt_iv');
            $response = [
                'p' => (new EncryptUtils())->openssl_en($response, $k, $i)];
        }
        return response($response);
    }
}