封装优化

This commit is contained in:
阿不叮咚
2025-09-24 15:38:44 +08:00
parent c272fc80b8
commit edd4effc95
14 changed files with 493 additions and 21 deletions

View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Aether;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\Rpc\Protocol;
use Psr\Http\Message\ResponseInterface;
use Throwable;
use Hyperf\Context\Context;
class RpcExceptionHandler extends ExceptionHandler
{
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
{
// 微服务间调用返回更精简的错误信息
$data = [
'code' => $throwable->getCode() ?: 500,
'message' => $throwable->getMessage() ?: '服务调用失败',
'request_id' => Context::get('request_id', ''),
];
$protocol = make(Protocol::class);
$response->getBody()->write($protocol->pack($data));
return $response;
}
public function isValid(Throwable $throwable): bool
{
return true;
}
}