框架支持异常处理由开发者自定义类进行接管,需要在app目录下面的provider.php文件中绑定异常处理类,例如:
Request::class,
// 'thinkexceptionHandle' => ExceptionHandle::class,//原来的异常
'thinkexceptionHandle' => '\app\exception\ExecptionHandle'
];
然后再app下面新建一个exception文件夹放自定义错误信息
msg = $e->getMessage() ?: $this->msg;
//HttpExceptions是继承同级目录下HttpExceptions,代码在下方
if ($e instanceof HttpExceptions) {
$this->httpCode = $e->getStatusCode() ?: $this->httpCode;
}
$this->errorCode = $e->getCode() ?: $this->errorCode;
$result_data = [
'message' => $this->msg,
'data' => [],
'code' => $this->errorCode
];
return json($result_data, $this->httpCode);
// 其他错误交给系统处理
return parent::render($request, $e);
}
}
同级目录下HttpExceptions 文件
statusCode = $statusCode;
$this->headers = $headers;
parent::__construct($message, $code, $previous);
}
public function getStatusCode()
{
return $this->statusCode;
}
public function getHeaders()
{
return $this->headers;
}
}
调用
use appexceptionHttpExceptions; throw new HttpExceptions(404,"自定义错误",'444');



