Laravel 自定义异常类

  • 方法一
    新增CustomException.php文件
    App\Exceptions\CustomException.php
<?php

namespace App\Exceptions;

use Exception;
/**
 * Carver自定义异常基类
 * Class BaseException
 * @package App\Exceptions
 */
class CustomException extends Exception
{
    /**
     * 状态码
     * @var int|mixed
     */
    public $code = 200;

    /**
     * 错误具体信息
     * @var mixed|string
     */
    public $message = 'json';

    /**
     * 构造函数,接收关联数组
     * BaseException constructor.
     * @param array $params
     */
    public function __construct($params = [])
    {
        parent::__construct();
        if (!is_array($params)) {
            return ;
        }
        if (array_key_exists('code', $params)) {
            $this->code = $params['code'];
        }
        if (array_key_exists('msg', $params)) {
            $this->message = $params['msg'];
        }
    }

    public function report()
    {
        //
    }
    public function render($request)
    {
        $result = [
            'code'  => $this->code,
            'msg'   => $this->message,
        ];
        //记录日志
//        Log::error($this->message);
        if($request->ajax()){
            return response()->json($result)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
        }else{
            $params = [
                'msg'  => $this->message,
                'wait' => 3,
                'url'  => 'javascript:history.back(-1);',
            ];
            return response()->view('common.error', $params, 500);
        }
    }
}
  • 方法二
    1.新增CustomException.php文件
    App\Exceptions\CustomException.php
<?php

namespace App\Exceptions;

/**
 * Carver自定义异常基类
 * Class BaseException
 * @package App\Exceptions\Custom
 */
class CustomException extends \Exception
{
    /**
     * 状态码
     * @var int|mixed
     */
    public $code = 200;

    /**
     * 错误具体信息
     * @var mixed|string
     */
    public $message = 'json';

    /**
     * 构造函数,接收关联数组
     * BaseException constructor.
     * @param array $params
     */
    public function __construct($params = [])
    {
        parent::__construct();
        if (!is_array($params)) {
            return ;
        }
        if (array_key_exists('code', $params)) {
            $this->code = $params['code'];
        }
        if (array_key_exists('msg', $params)) {
            $this->message = $params['msg'];
        }
    }


}
  • 2.修改render()方法
    App\Exceptions\Handler.php
public function render($request, Exception $exception)
    {
        if ($exception instanceof CustomException) {
            // 如果是自定义的异常
            $this->code    = $exception->code;
            $this->message = $exception->message;
            $result = [
                'code'  => $this->code,
                'msg'   => $this->message,
            ];
            //记录日期
            Log::error($exception->message);
            if($request->ajax()){
                return response()->json($result)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
            }else{
                $params = [
                    'msg'  => $this->message,
                    'wait' => 3,
                    'url'  => 'javascript:history.back(-1);',
                ];
                return response()->view('common.error', $params, 500);
            }
        }
        
        return parent::render($request, $exception);
    }

测试#

throw  new \App\Exceptions\CustomException(['msg'=>'Carver自定义错误','code'=>400]);

  

 

作者:Carver-大脸猫

出处:https://www.cnblogs.com/carver/articles/17660164.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

转载请注明原处

posted @   Carver-大脸猫  阅读(115)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up light_mode palette
选择主题
menu