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

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