Files
CompanyProject/滴滴淘/diditao/doc/exception.md
2024-09-27 01:32:49 +08:00

56 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 异常机制
系统提供三种异常机制:
### 逻辑异常
逻辑异常为业务逻辑发生的异常,非预期的业务逻辑结果均可认为是异常,如变量校验、不被允许的操作等等,对于此类异常,可自行创建类似以下的异常类:
```
namespace diditao\user\exception;
use diditao\common\exception\AppError;
class BuyerRegException extends AppError
{
protected $exceptionKey='reg failed';
protected $status = 11000;
}
```
其中,`$exceptionKey``exception.php`中定义的key`$status`为一个能被100整除的整数为规范期间公共部分接口为四位数单独某个服务为五位数。
使用方法:
```
throw new BuyerRegException(string $appMsg = null,int $status=null,$appData=null,$errorData=null);
```
> $appMsg直接抛向前端的语义化错误提示
>
> $status最终抛向前端的status注意此处为追加非替换即如果要抛出11005直接设置$status=5即可。
>
>$appData:为抛向前端的data部分
>
>$errorData系统日志记录数据
### 可预知的系统异常
可预知的系统异常比如不常见的redis异常IO异常、mysql异常、第三方API异常等针对此类异常可以抛出可预知的系统异常示例代码如下
```
throw new AppException(string $exceptionKey,string $msg,\Exception $e=null, int $errorLevel = 0);
```
> $exceptionKey为`exception.php`中定义的key
>
> $msg直接抛向前端的语义化错误提示
>
> $e接管的异常实例
>
> $errorLevel错误级别数字越大代表异常级别越高系统会针对不同的异常级别触发不同的警告
### 不可预知的系统异常
针对不可预知的系统异常,系统会抛出框架异常,并返回给前端统一的错误提示`系统异常,请稍后再试`