1、新建文件夹以及文件
路径: /application/lib/exception/ExceptionHandler.php
并写入以下代码:
<?php
namespace app\lib\exception;
use think\Exception;
use think\config;
use think\exception\Handle;
/**
* 自定义异常类
*/
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,
'errorcode'=>'100000',
'msg' => $message,
'result' =>$data
];
return json($data, $httpCode);
}
}
为了便于调试,引入config文件。
debug开启时候,会正常显示错误信息,debug关闭,则是json数据。
2、修改 config.php中的 exception_handle , 接管异常处理
// 异常处理handle类 留空使用 \think\exception\Handle
'exception_handle' => '\app\lib\exception\ExceptionHandler',