This commit is contained in:
Aether
2025-09-28 14:51:38 +08:00
parent afd384a52a
commit 10b58a8c6d
8 changed files with 326 additions and 4 deletions

View File

@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace App\Exception\Handler;
use Aether\AetherResponse;
use App\Exception\BusinessException;
use Aether\RpcException\BusinessException;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Psr\Http\Message\ResponseInterface;

View File

@@ -1,13 +1,12 @@
<?php
declare(strict_types=1);
use Aether\RpcExceptionHandler;
use Aether\RpcException\JsonRpcExceptionHandler;
return [
'handler' => [
'jsonrpc-http' => [
RpcExceptionHandler::class,
JsonRpcExceptionHandler::class,
],
],
];

View File

@@ -0,0 +1,96 @@
<?php
declare(strict_types=1);
namespace Aether\Middleware;
use Aether\RpcException\BusinessException;
use Aether\RpcException\ErrorCode;
use Hyperf\Context\ApplicationContext;
use Hyperf\Context\Context;
use Hyperf\Contract\StdoutLoggerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Throwable;
use function Hyperf\support\env;
class GatewayExceptionHandler implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
Context::set(ServerRequestInterface::class, $request);
try {
return $handler->handle($request);
} catch (Throwable $e) {
// 尝试解析异常信息判断是否为RPC服务返回的业务异常
$parsed = $this->parseRpcException($e);
if ($parsed) {
// 转换为业务异常
throw new BusinessException(
$parsed['code'],
$parsed['message'],
$parsed['data'] ?? []
);
}
// 记录非业务异常日志
$logger = ApplicationContext::getContainer()->get(StdoutLoggerInterface::class);
$logger->error(sprintf('服务调用异常: %s', $e->getMessage()), [
'trace' => $e->getTraceAsString(),
'file' => $e->getFile(),
'line' => $e->getLine(),
]);
// 非业务异常,使用系统错误码
if ($e instanceof BusinessException) {
throw $e;
}
// 统一转换为系统错误
throw new BusinessException(
env('APP_ENV') === 'dev' ? $e->getMessage() : ErrorCode::getMessage(ErrorCode::RPC_CALL_ERROR),
ErrorCode::RPC_CALL_ERROR,
);
}
}
/**
* 解析RPC异常信息.
*/
private function parseRpcException(Throwable $e): ?array
{
try {
// 从异常消息中解析JSON数据
$message = $e->getMessage();
$data = json_decode($message, true);
// 检查是否为有效的JSON-RPC错误响应
if (json_last_error() === JSON_ERROR_NONE && isset($data['jsonrpc']) && $data['jsonrpc'] === '2.0') {
$error = $data['error'] ?? [];
// 检查是否包含业务异常标识
return [
'code' => $error['code'] ?? ErrorCode::RPC_CALL_ERROR,
'message' => $error['message'] ?? '服务调用异常',
'data' => $error['data'] ?? [],
];
// 普通RPC错误
}
// 检查是否为直接返回的错误数组
if (is_array($data) && isset($data['code'], $data['message'])) {
return $data;
}
} catch (Throwable $parseError) {
// 解析失败不影响主流程
}
return null;
}
}

View File

@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace Aether\RpcException;
use Aether\Config;
use App\Exception\BusinessException;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\ResponseInterface;
use Throwable;
use function Hyperf\support\env;
class ApiExceptionHandler extends ExceptionHandler
{
public function handle(Throwable $throwable, ResponseInterface $response): MessageInterface|ResponseInterface
{
// 业务异常
if ($throwable instanceof BusinessException) {
$code = $throwable->getCode();
$message = $throwable->getMessage();
$data = $throwable->getData();
} else {
// 其他异常
$code = $throwable->getCode(); // ErrorCode::SYSTEM_ERROR;
$data = env('APP_ENV') === 'dev' ? [
'trace' => $throwable->getTraceAsString(),
'file' => $throwable->getFile(),
'line' => $throwable->getLine(),
] : [];
$message = env('APP_ENV') === 'dev' ? $throwable->getMessage() : ErrorCode::getMessage($code);
}
$result = [
Config::RESPONSE_FIELD_KEY_CODE => $code,
Config::RESPONSE_FIELD_KEY_DATA => $data,
Config::RESPONSE_FIELD_KEY_MESSAGE => $message,
];
$body = new SwooleStream(json_encode($result, JSON_UNESCAPED_UNICODE));
return $response->withHeader('Content-Type', 'application/json')
->withStatus(200)
->withBody($body);
}
public function isValid(Throwable $throwable): bool
{
return true;
}
}

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Aether\RpcException;
use Hyperf\Server\Exception\ServerException;
use Throwable;
class BusinessException extends ServerException
{
/**
* @var int 错误码
*/
protected $code;
/**
* @var array 额外数据
*/
protected array $data = [];
public function __construct(string $message = '', int $code = ErrorCode::SYSTEM_ERROR, array $data = [], ?Throwable $previous = null)
{
if (empty($message)) {
$message = ErrorCode::getMessage($code);
}
$this->code = $code;
$this->data = $data;
parent::__construct($message, $code, $previous);
}
/**
* 获取额外数据.
*/
public function getData(): array
{
return $this->data;
}
}

View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace Aether\RpcException;
class ErrorCode
{
// 系统错误
public const SYSTEM_ERROR = 500;
public const PARAM_ERROR = 400;
public const AUTH_ERROR = 401;
public const FORBIDDEN_ERROR = 403;
public const NOT_FOUND = 404;
// 业务错误
public const DATA_NOT_FOUND = 10001;
public const ARTICLE_NOT_FOUND = 20001;
public const NOTICE_NOT_FOUND = 30001;
public const RPC_CALL_ERROR = 50001;
/**
* 获取错误信息.
*/
public static function getMessage(int $code): string
{
$messages = [
self::SYSTEM_ERROR => '系统错误',
self::PARAM_ERROR => '参数错误',
self::AUTH_ERROR => '未授权',
self::FORBIDDEN_ERROR => '权限不足',
self::NOT_FOUND => '资源不存在',
self::DATA_NOT_FOUND => '数据不存在',
self::ARTICLE_NOT_FOUND => '文章不存在',
self::NOTICE_NOT_FOUND => '公告不存在',
];
return $messages[$code] ?? '未知错误';
}
}

View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace Aether\RpcException;
use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\ConfigInterface;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\Rpc\Contract\ResponseInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\MessageInterface;
use Throwable;
class JsonRpcExceptionHandler
{
/**
* JSON-RPC 异常处理.
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(Throwable $throwable, ResponseInterface $response): MessageInterface|\Psr\Http\Message\ResponseInterface|ResponseInterface
{
$responseContents = $response->getBody()->getContents();
$responseContents = json_decode($responseContents, true);
if (! empty($responseContents['error'])) {
$port = null;
$config = ApplicationContext::getContainer()->get(ConfigInterface::class);
$servers = $config->get('server.servers');
foreach ($servers as $k => $server) {
if ($server['name'] == 'jsonrpc-http') {
$port = $server['port'];
break;
}
}
$responseContents['error']['message'] .= " - {$config->get('app_name')}:{$port}";
}
$data = json_encode($responseContents, JSON_UNESCAPED_UNICODE);
return $response->withStatus(200)->withBody(new SwooleStream($data));
}
public function isValid(Throwable $throwable): bool
{
return true;
}
}

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Aether\RpcException;
use Hyperf\Server\Exception\ServerException;
use Throwable;
class ValidateException extends ServerException
{
/**
* @var int 错误码
*/
protected $code;
/**
* @var array 额外数据
*/
protected array $data = [];
public function __construct(string $message = '', int $code = ErrorCode::PARAM_ERROR, array $data = [], ?Throwable $previous = null)
{
if (empty($message)) {
$message = ErrorCode::getMessage($code);
}
$this->code = $code;
$this->data = $data;
parent::__construct($message, $code, $previous);
}
public function getData(): array
{
return $this->data;
}
}