在处理逻辑代码中我们经常会碰到逻辑判断提示 或者token无效,操作失败,服务内部错误,这个时候我们需要停止程序并且返回提示和状态码
1.新建异常基类
extend/exception/BaseException.php
<?php
/**
* Created by PhpStorm.
* User: Pasa吴
* Date: 2022/6/5
* Time: 12:46
*/
namespace exception;
use think\Exception;
class BaseException extends Exception
{
public $code = 200;
public $msg = 'invalid parameters';
public $errorCode = 0;
public $data = '';
/**
* 构造函数,接收一个关联数组
* @param array $params 关联数组只应包含code、msg和errorCode,且不应该是空值
*/
public function __construct($params = [])
{
if (!is_array($params)) {
return;
}
if (array_key_exists('code', $params)) {
$this->code = $params['code'];
}
if (array_key_exists('msg', $params)) {
$this->msg = $params['msg'];
$this->message = $params['msg'];
}
if (array_key_exists('message', $params)) {
$this->message = $params['message'];
}
if (array_key_exists('errorCode', $params)) {
$this->errorCode = $params['errorCode'];
}
if (array_key_exists('data', $params)) {
$this->data = $params['data'];
}
}
}
2.新建API异常类
extend/exception/ApiException.php
<?php
/**
* Created by PhpStorm.
* User: Pasa吴
* Date: 2022/6/5
* Time: 12:46
*/
namespace exception;
use think\Exception;
class BaseException extends Exception
{
public $code = 200;
public $msg = 'invalid parameters';
public $errorCode = 0;
public $data = '';
/**
* 构造函数,接收一个关联数组
* @param array $params 关联数组只应包含code、msg和errorCode,且不应该是空值
*/
public function __construct($params = [])
{
if (!is_array($params)) {
return;
}
if (array_key_exists('code', $params)) {
$this->code = $params['code'];
}
if (array_key_exists('msg', $params)) {
$this->msg = $params['msg'];
$this->message = $params['msg'];
}
if (array_key_exists('message', $params)) {
$this->message = $params['message'];
}
if (array_key_exists('errorCode', $params)) {
$this->errorCode = $params['errorCode'];
}
if (array_key_exists('data', $params)) {
$this->data = $params['data'];
}
}
}
3.异常处理类
extend/exception/Http.php
<?php
/**
* Created by PhpStorm.
* User: Pasa吴
* Date: 2022/6/5
* Time: 13:18
*/
namespace exception;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\facade\Request;
use think\Response;
use Throwable;
class Http extends Handle
{
public function render($request, Throwable $e): Response
{
// 添加自定义异常处理机制
if ($e instanceof BaseException) {
//如果是自定义异常,则控制http状态码,不需要记录日志
//因为这些通常是因为客户端传递参数错误或者是用户请求造成的异常
//不应当记录日志
$this->code = $e->code;
$this->msg = $e->msg;
$this->errorCode = $e->errorCode;
$result = ['code' => $this->errorCode, 'msg' => $this->msg, 'data' => $e->data];
return json($result, $this->code);
}
// 参数验证错误
if ($e instanceof ValidateException) {
$result = ['code' => $e->getCode(), 'msg' => $e->getError(), 'data' => []];
return json($result, 200);
}
// 请求异常
if ($e instanceof HttpException) {
$this->msg = $e->getMessage();
$this->code = 200;
$this->errorCode = 0;
$result = ['code' => $this->errorCode, 'msg' => $this->msg, 'data' => Request::instance()->url()];
return json($result, $this->code);
}
// 其他错误交给系统处理
return parent::render($request, $e);
}
}
4.异常处理接管
TP6框架支持异常处理由开发者自定义类进行接管,需要在app目录下面的provider.php文件中绑定异常处理类
<?php
use app\ExceptionHandle;
use app\Request;
// 容器Provider定义文件
return [
'think\Request' => Request::class,
'think\exception\Handle' => \exception\Http::class,
];
5.抛异常
<?php
/**
* Created by PhpStorm.
* User: Pasa吴
* Date: 2022/6/4
* Time: 23:19
*/
namespace app\api\controller;
use curd\BuildCurd;
use exception\ServiceException;
class Test
{
public function test()
{
throw new ServiceException(['异常提示']);
}
}
本文为Pasa吴原创文章,转载无需和我联系,但请注明来自Pasa吴博客https://www.pasawu.top
最新评论