Laravel框架统一异常捕获并返回自定义错误信息

Laravel框架中,App\Exceptions\Hander.php 为异常捕获的统一处理地方,可以在这里自定义你想处理的任何错误信息,并友好的返回自定义错误信息。主要涉及到2个方法,report 和 render。如下图所示,render方法里为自定义捕获的错误类型,返回数据以及状态码可根据自己爱好自定义,下面为常见的一些错误类型捕获:

<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use Illuminate\Database\QueryException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class Handler extends ExceptionHandler
{
    /**
     * The list of the inputs that are never flashed to the session on validation exceptions.
     *
     * @var array<int, string>
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     */
    public function register(): void
    {
        $this->reportable(function (Throwable $e) {
            //
        });
    }


    public function report(Throwable $exception)
    {
        parent::report($exception);
    }


    public function render($request, Throwable $exception){

        $code = 0;
        $msg = '';
        if ($exception instanceof HttpException) {

            $code = 500;
            $msg = 'http请求错误,请稍后重试';
        }else if ($exception instanceof NotFoundHttpException) {

            $code = 404;
            $msg = '访问的地址不存在';
        }else if ($exception instanceof MethodNotAllowedHttpException) {

             $code = 405;
             $msg = 'http请求方式不被允许,请检查后重试';
         }else if ($exception instanceof QueryException) {

            $code = 500;
            $msg = '数据处理错误,请稍后重试';
        }else if ($exception instanceof ModelNotFoundException) {

            $code = 404;
            $msg = '模型未找到,请稍后重试';

        } else if ($exception instanceof \ReflectionException) {

            $code = 500;
            $msg = '项目反射类/方法/函数错误,请稍后重试';
        }else if ($exception instanceof \ErrorException) {

            $code = 500;
            $msg = '发生error错误,请稍后重试';
        }else if ($exception instanceof \Runtimeexception){

            $code = 500;
            $msg = '程序运行出错,请稍后重试';
        }

        if ($code > 0) {
            Log::info('错误:',['code'=>$code,'msg'=>$msg,'data'=>$exception->getMessage()]);
            return response()->json(['code'=>$code,'message'=>$msg], $code);
        }
        return parent::render($request, $exception);

    }


}

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值