This commit is contained in:
2026-07-20 14:59:16 +08:00
parent cd1175b304
commit 77930b3232
9 changed files with 94 additions and 78 deletions

View File

@@ -1,4 +1,5 @@
<?php
namespace app;
use think\db\exception\DataNotFoundException;
@@ -9,12 +10,15 @@ use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\Response;
use Throwable;
use app\common\Trait\ApiResponse;
/**
* 应用异常处理类
*/
class ExceptionHandle extends Handle
{
use ApiResponse;
/**
* 不需要记录信息(日志)的异常类列表
* @var array
@@ -50,7 +54,20 @@ class ExceptionHandle extends Handle
*/
public function render($request, Throwable $e): Response
{
// 添加自定义异常处理机制
// 验证器异常
if ($e instanceof ValidateException) {
return $this->error($e->getError(), 422); // 验证器没通过
};
// 请求异常
if ($e instanceof HttpException) {
return $this->error($e->getMessage(), $e->getStatusCode());
};
// 处理【自定义业务异常】(在 Service 层主动 throw 的错误)
if ($e instanceof \think\Exception) {
return $this->error($e->getMessage(), $e->getCode() ?: 400);
}
// 其他错误交给系统处理
return parent::render($request, $e);