1. 调整异常处理类

2. 统一职责分工
3. 清除多余代码
This commit is contained in:
技术老胡
2026-02-24 13:37:35 +08:00
parent d29751cce8
commit d5a134d3a8
59 changed files with 3370 additions and 646 deletions

View File

@@ -0,0 +1,25 @@
<?php
namespace app\exceptions;
use Webman\Exception\BusinessException;
/**
* 请求参数错误异常
* 用于请求参数格式错误、验证码错误等情况
*
* 示例:
* throw new BadRequestException('验证码错误或已失效');
* throw new BadRequestException('请求参数格式错误');
*/
class BadRequestException extends BusinessException
{
public function __construct(string $message = '请求参数错误', int $bizCode = 400, array $data = [])
{
parent::__construct($message, $bizCode);
if (!empty($data)) {
$this->data($data);
}
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace app\exceptions;
use Webman\Exception\BusinessException;
/**
* 禁止访问异常
* 用于无权限、账号被禁用等情况
*
* 示例:
* throw new ForbiddenException('账号已被禁用');
* throw new ForbiddenException('无权限访问该资源');
*/
class ForbiddenException extends BusinessException
{
public function __construct(string $message = '禁止访问', int $bizCode = 403, array $data = [])
{
parent::__construct($message, $bizCode);
if (!empty($data)) {
$this->data($data);
}
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace app\exceptions;
use Webman\Exception\BusinessException;
/**
* 系统内部错误异常
* 用于配置文件错误、系统错误等不可预期的错误
*
* 示例:
* throw new InternalServerException('字典配置文件不存在');
* throw new InternalServerException('配置文件格式错误:' . json_last_error_msg());
* throw new InternalServerException('保存失败');
*/
class InternalServerException extends BusinessException
{
public function __construct(string $message = '系统内部错误', int $bizCode = 500, array $data = [])
{
parent::__construct($message, $bizCode);
if (!empty($data)) {
$this->data($data);
}
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace app\exceptions;
use Webman\Exception\BusinessException;
/**
* 资源不存在异常
* 用于资源未找到的情况
*
* 示例:
* throw new NotFoundException('用户不存在');
* throw new NotFoundException('未找到指定的字典:' . $code);
*/
class NotFoundException extends BusinessException
{
public function __construct(string $message = '资源不存在', int $bizCode = 404, array $data = [])
{
parent::__construct($message, $bizCode);
if (!empty($data)) {
$this->data($data);
}
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace app\exceptions;
use Webman\Exception\BusinessException;
/**
* 未授权异常
* 用于认证失败、token无效等情况
*
* 示例:
* throw new UnauthorizedException('账号或密码错误');
* throw new UnauthorizedException('认证令牌已过期');
* throw new UnauthorizedException('认证令牌无效');
*/
class UnauthorizedException extends BusinessException
{
public function __construct(string $message = '未授权', int $bizCode = 401, array $data = [])
{
parent::__construct($message, $bizCode);
if (!empty($data)) {
$this->data($data);
}
}
}

View File

@@ -3,8 +3,6 @@
namespace app\exceptions;
use Webman\Exception\BusinessException;
use Webman\Http\Request;
use Webman\Http\Response;
/**
* 参数校验异常
@@ -19,6 +17,9 @@ class ValidationException extends BusinessException
{
public function __construct(string $message = '参数校验失败', int $bizCode = 422, array $data = [])
{
parent::__construct($message, $bizCode, $data);
parent::__construct($message, $bizCode);
if (!empty($data)) {
$this->data($data);
}
}
}