封装优化
This commit is contained in:
34
extend/Aether/PHP/Hyperf/RpcExceptionHandler.php
Normal file
34
extend/Aether/PHP/Hyperf/RpcExceptionHandler.php
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user