一、创建自定义异常类基类 app/lib/exception/BaseException.php
<?php
namespace app\lib\exception;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\Response;
use Throwable;
use think\facade\Env;
class ExceptionHandler extends Handle
{
public $code;
public $msg;
public $errorCode;
public function render($request, Throwable $e): Response
{
if ($e instanceof BaseException) {
$this->code = $e->code;
$this->msg = $e->msg;
$this->errorCode = $e->errorCode;
}else{
// debug开启,显示默认的异常
if (Env::get('APP_DEBUG')) return parent::render($request, $e);
//halt(Env::get('APP_DEBUG'));
$this->code = 500;
$this->msg = '服务器异常';
$this->errorCode = '999';
}
$res = [
'msg'=&g