thinkphp5自定义异常处理类 异常接管

配置文件里设置异常处理类:

   // 异常处理handle类 留空使用 \think\exception\Handle
    'exception_handle'       => '\think\exception\ExceptionHandler', 

ExceptionHandler.php文件如下:

<?php
namespace think\exception;

use think\Exception;
use think\config;
use think\exception\Handle;
/**
 * 自定义异常类 jee
 */
class ExceptionHandler extends Handle {
    
    
    /**
     * http状态码
     * @var unknown
     */
    public $httpCode = 500;

    public function render(\Exception $e){
    
    
        $debug_status = config("app_debug");
        if($debug_status){
    
    
            return parent::render($e);
        }else{
    
    
            return $this->show(2, $e->getMessage(), [], $this->httpCode);
        }

    }

    /**
     * 通用化API接口数据输出
     * @param int $status  操作成功还是失败: 1 成功 2 失败
     * @param int $errorcode 业务错误状态码
     * @param string $msg 信息提示
     * @param [] $result 数据
     * @param int $httpCode http状态码
     */
    public function show($status, $message ,$data = [] ,$httpCode = 200)
    {
    
    
        $data =  [
//            'status' => $status,
            'code'=>'500',
            'msg' => $message,
            'data' =>$data
        ];

        return json($data, $httpCode);

    }
}

我的微信:

猜你喜欢

转载自blog.csdn.net/jeesr/article/details/118995688
今日推荐