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,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;
}
}