This commit is contained in:
Aether
2025-09-18 10:46:54 +08:00
commit 0920cef866
62 changed files with 13547 additions and 0 deletions

View File

@@ -0,0 +1,136 @@
<?php
declare(strict_types=1);
namespace Aether\Exception;
use Aether\AetherValidator;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Database\Model\ModelNotFoundException; // 引入模型未找到异常
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\Validation\ValidationException;
use Psr\Http\Message\ResponseInterface;
use Throwable;
use Hyperf\Context\Context;
use function Hyperf\Support\env;
class AetherExceptionHandler extends ExceptionHandler
{
public function __construct(protected StdoutLoggerInterface $logger)
{
}
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
{
$requestId = Context::get('request_id', uniqid());
$result = $this->formatErrorResponse($throwable, $requestId);
// 记录错误日志(包含完整堆栈)
$this->logger->error(sprintf(
'Exception [%s] | RequestId: %s | Message: %s in %s:%d',
get_class($throwable),
$requestId,
$throwable->getMessage(),
$throwable->getFile(),
$throwable->getLine()
));
if (env('APP_ENV') === 'dev') {
$this->logger->error($throwable->getTraceAsString());
}
// 确保状态码合法100-599
$statusCode = $result['code'] ?? 500;
if ($statusCode < 100 || $statusCode >= 600) {
$statusCode = 500;
}
// 构建响应
return $response
->withHeader('Content-Type', 'application/json')
->withStatus($statusCode)
->withBody(new SwooleStream(json_encode($result, JSON_UNESCAPED_UNICODE)));
}
private function formatErrorResponse(Throwable $throwable, string $requestId): array
{
// 模型未找到异常
if ($throwable instanceof ModelNotFoundException) {
return [
'code' => 404, // 资源不存在标准状态码
'message' => $throwable->getMessage() ?: '请求的资源不存在',
'data' => env('APP_ENV') === 'dev' ? [
'file' => $throwable->getFile(),
'line' => $throwable->getLine()
] : null,
'request_id' => $requestId,
'timestamp' => time()
];
}
// 自定义验证异常
if ($throwable instanceof ValidationFailedException) {
return $this->formatValidationError($throwable, $requestId);
}
// 原生验证异常
if ($throwable instanceof ValidationException) {
return $this->formatNativeValidationError($throwable, $requestId);
}
// 其他异常如RuntimeException等
return [
'code' => 600,
'message' => env('APP_ENV') === 'dev' ? $throwable->getMessage() : '服务暂时不可用',
'data' => env('APP_ENV') === 'dev' ? [
'file' => $throwable->getFile(),
'line' => $throwable->getLine(),
'trace' => explode("\n", $throwable->getTraceAsString())
] : null,
'request_id' => $requestId,
'timestamp' => time()
];
}
private function formatValidationError(ValidationFailedException $e, string $requestId): array
{
$validatorInstance = new class extends AetherValidator {
protected function scenes(): array { return []; }
};
return [
'code' => 422,
'message' => $e->getMessage(),
'data' => [
'errors' => $validatorInstance->formatValidationErrors($e->validator),
'scene' => $e->getScene(),
'validated_data' => env('APP_ENV') === 'dev' ? $e->validator->getData() : null
],
'request_id' => $requestId,
'timestamp' => time()
];
}
private function formatNativeValidationError(ValidationException $e, string $requestId): array
{
$validatorInstance = new class extends AetherValidator {
protected function scenes(): array { return []; }
};
return [
'code' => 422,
'message' => '参数验证失败',
'data' => [
'errors' => $validatorInstance->formatValidationErrors($e->validator),
'validated_data' => env('APP_ENV') === 'dev' ? $e->validator->getData() : null
],
'request_id' => $requestId,
'timestamp' => time()
];
}
public function isValid(Throwable $throwable): bool
{
return true;
}
}

View File

@@ -0,0 +1,119 @@
<?php
declare(strict_types=1);
namespace Aether\Exception;
use Aether\AetherValidator;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\Validation\ValidationException;
use Psr\Http\Message\ResponseInterface;
use Throwable;
use Hyperf\Context\Context;
use function Hyperf\Support\env;
class AppExceptionHandler extends ExceptionHandler
{
public function __construct(protected StdoutLoggerInterface $logger)
{
}
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
{
$requestId = Context::get('request_id', uniqid());
$result = $this->formatErrorResponse($throwable, $requestId);
$this->logger->error(sprintf(
'Exception: %s[%s] in %s:%d',
get_class($throwable),
$throwable->getMessage(),
$throwable->getFile(),
$throwable->getLine()
));
return $response
->withHeader('Content-Type', 'application/json')
->withStatus($result['code'] ?? 500)
->withBody(new SwooleStream(json_encode($result, JSON_UNESCAPED_UNICODE)));
}
/**
* 统一错误响应格式
*/
private function formatErrorResponse(Throwable $throwable, string $requestId): array
{
// 处理自定义验证异常
if ($throwable instanceof ValidationFailedException) {
return $this->formatValidationError($throwable, $requestId);
}
// 处理原生验证异常
if ($throwable instanceof ValidationException) {
return $this->formatNativeValidationError($throwable, $requestId);
}
// 处理其他异常
return [
'code' => 500,
'message' => env('APP_ENV') === 'dev' ? $throwable->getMessage() : '服务暂时不可用',
'data' => env('APP_ENV') === 'dev' ? [
'file' => $throwable->getFile(),
'line' => $throwable->getLine(),
'trace' => explode("\n", $throwable->getTraceAsString())
] : null,
'request_id' => $requestId,
'timestamp' => time()
];
}
/**
* 格式化自定义验证异常
*/
private function formatValidationError(ValidationFailedException $e, string $requestId): array
{
// 复用AetherValidator的错误格式化方法
$validatorInstance = new class extends AetherValidator {
protected function scenes(): array { return []; }
};
return [
'code' => 422,
'message' => $e->getMessage(),
'data' => [
'errors' => $validatorInstance->formatValidationErrors($e->validator),
'scene' => $e->getScene(), // 直接从异常获取场景
'validated_data' => env('APP_ENV') === 'dev' ? $e->validator->getData() : null
],
'request_id' => $requestId,
'timestamp' => time()
];
}
/**
* 格式化原生验证异常(保持格式一致)
*/
private function formatNativeValidationError(ValidationException $e, string $requestId): array
{
$validatorInstance = new class extends AetherValidator {
protected function scenes(): array { return []; }
};
return [
'code' => 422,
'message' => '参数验证失败',
'data' => [
'errors' => $validatorInstance->formatValidationErrors($e->validator),
'validated_data' => env('APP_ENV') === 'dev' ? $e->validator->getData() : null
],
'request_id' => $requestId,
'timestamp' => time()
];
}
public function isValid(Throwable $throwable): bool
{
return true;
}
}

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace Aether\Exception;
use Hyperf\Server\Exception\ServerException;
class BusinessException extends ServerException
{
// 错误码常量(按业务模块划分)
public const VALIDATION_ERROR = 400; // 参数验证失败
public const AUTH_ERROR = 401; // 认证失败
public const PERMISSION_DENY = 403; // 权限不足
public const RESOURCE_NOT_FOUND = 404; // 资源不存在
public const SCENE_NOT_FOUND = 400;
/**
* 额外错误数据(如验证详情)
*/
protected ?array $errorData = null;
public function __construct(
string $message,
int $code = 500,
?\Throwable $previous = null,
?array $errorData = null
) {
parent::__construct($message, $code, $previous);
$this->errorData = $errorData;
}
/**
* 获取额外错误数据
*/
public function getErrorData(): ?array
{
return $this->errorData;
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace Aether\Exception;
use Hyperf\Validation\ValidationException;
use Hyperf\Validation\Validator;
use Psr\Http\Message\ResponseInterface;
class ValidationFailedException extends ValidationException
{
/**
* 验证场景
*/
protected string $scene;
public function __construct(
Validator $validator,
string $scene,
string $message = '参数验证失败',
?ResponseInterface $response = null // 新增response参数符合父类要求
) {
// 父类构造函数仅接受 $validator 和 $response
parent::__construct($validator, $response);
// 单独设置消息(父类的 $message 为 protected 属性,可直接赋值)
$this->message = $message;
$this->scene = $scene;
}
public function getScene(): string
{
return $this->scene;
}
}