Files
hyperf_data/extend/Aether/PHP/Hyperf/RpcExceptionHandler.php
阿不叮咚 edd4effc95 封装优化
2025-09-24 15:38:44 +08:00

34 lines
868 B
PHP

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