TP5.1自定义异常处理类
在开发API接口中,需要对接口有错误信息返回,所以需要建一个在全局都能使用的自定义异常类。
在common目录下新建exception/ExceptionHandler.php
代码内容如下:
<?php
namespace app\common\exception;
use app\common\utils\ResponseHelper;
use think\exception\Handle;
use think\Request;
use Exception;
class ExceptionHandler extends Handle
{
protected $request;
public function __construct()
{
$this->request = new Request();
}
public function render(Exception $e)
{
if (strtolower($this->request->header('sec-fetch-mode') ? $this->request->header('sec-fetch-mode') : '') == 'cors'
&& strtolower($this->request->method()) == 'options') {
return ResponseHelper::response('cors success');
} else {
return ResponseHelper::error($e);
}
}
}
新建 uti