mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-02 11:48:08 +08:00
change jsonrpc error code
This commit is contained in:
parent
1e4430c72d
commit
92a6707f0f
@ -46,13 +46,13 @@ class CoreMiddleware extends \Hyperf\RpcServer\CoreMiddleware
|
||||
$controllerInstance = $this->container->get($controller);
|
||||
if (! method_exists($controller, $action)) {
|
||||
// Route found, but the handler does not exist.
|
||||
return $this->responseBuilder->buildErrorResponse($request, -32603);
|
||||
return $this->responseBuilder->buildErrorResponse($request, ResponseBuilder::INTERNAL_ERROR);
|
||||
}
|
||||
$parameters = $this->parseParameters($controller, $action, $request->getParsedBody());
|
||||
try {
|
||||
$response = $controllerInstance->{$action}(...$parameters);
|
||||
} catch (\Exception $e) {
|
||||
return $this->responseBuilder->buildErrorResponse($request, -32603, $e);
|
||||
return $this->responseBuilder->buildErrorResponse($request, ResponseBuilder::SERVER_ERROR, $e);
|
||||
}
|
||||
}
|
||||
return $response;
|
||||
@ -60,7 +60,7 @@ class CoreMiddleware extends \Hyperf\RpcServer\CoreMiddleware
|
||||
|
||||
protected function handleNotFound(ServerRequestInterface $request)
|
||||
{
|
||||
return $this->responseBuilder->buildErrorResponse($request, -32601);
|
||||
return $this->responseBuilder->buildErrorResponse($request, ResponseBuilder::METHOD_NOT_FOUND);
|
||||
}
|
||||
|
||||
protected function handleMethodNotAllowed(array $routes, ServerRequestInterface $request)
|
||||
|
@ -72,12 +72,12 @@ class HttpServer extends Server
|
||||
Context::set(ResponseInterface::class, $psr7Response = new Psr7Response($response));
|
||||
if (! $this->isHealthCheck($psr7Request)) {
|
||||
if (strpos($psr7Request->getHeaderLine('content-type'), 'application/json') === false) {
|
||||
$psr7Response = $this->responseBuilder->buildErrorResponse($psr7Request, -32700);
|
||||
$psr7Response = $this->responseBuilder->buildErrorResponse($psr7Request, ResponseBuilder::PARSE_ERROR);
|
||||
}
|
||||
// @TODO Optimize the error handling of encode.
|
||||
$content = $this->packer->unpack($psr7Request->getBody()->getContents());
|
||||
if (! isset($content['jsonrpc'], $content['method'], $content['params'])) {
|
||||
$psr7Response = $this->responseBuilder->buildErrorResponse($psr7Request, -32600);
|
||||
$psr7Response = $this->responseBuilder->buildErrorResponse($psr7Request, ResponseBuilder::INVALID_REQUEST);
|
||||
}
|
||||
}
|
||||
$psr7Request = $psr7Request->withUri($psr7Request->getUri()->withPath($content['method'] ?? '/'))
|
||||
|
@ -21,6 +21,18 @@ use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class ResponseBuilder
|
||||
{
|
||||
const SERVER_ERROR = -32000;
|
||||
|
||||
const INVALID_REQUEST = -32600;
|
||||
|
||||
const METHOD_NOT_FOUND = -32601;
|
||||
|
||||
const INVALID_PARAMS = -32602;
|
||||
|
||||
const INTERNAL_ERROR = -32603;
|
||||
|
||||
const PARSE_ERROR = -32700;
|
||||
|
||||
/**
|
||||
* @var \Hyperf\Rpc\Contract\DataFormatterInterface
|
||||
*/
|
||||
@ -67,11 +79,11 @@ class ResponseBuilder
|
||||
protected function error(int $code, ?string $message = null): array
|
||||
{
|
||||
$mapping = [
|
||||
-32700 => 'Parse error.',
|
||||
-32600 => 'Invalid request.',
|
||||
-32601 => 'Method not found.',
|
||||
-32602 => 'Invalid params.',
|
||||
-32603 => 'Internal error.',
|
||||
self::PARSE_ERROR => 'Parse error.',
|
||||
self::INVALID_REQUEST => 'Invalid request.',
|
||||
self::METHOD_NOT_FOUND => 'Method not found.',
|
||||
self::INVALID_PARAMS => 'Invalid params.',
|
||||
self::INTERNAL_ERROR => 'Internal error.',
|
||||
];
|
||||
if (isset($mapping[$code])) {
|
||||
return [$code, $mapping[$code]];
|
||||
|
@ -84,7 +84,7 @@ class TcpServer extends Server
|
||||
->withAttribute('request_id', $data['id'] ?? null)
|
||||
->withParsedBody($data['params'] ?? '');
|
||||
if (! isset($data['jsonrpc'])) {
|
||||
return $this->responseBuilder->buildErrorResponse($request, -32600);
|
||||
return $this->responseBuilder->buildErrorResponse($request, ResponseBuilder::INVALID_REQUEST);
|
||||
}
|
||||
return $request;
|
||||
}
|
||||
|
@ -89,8 +89,8 @@ class CoreMiddlewareTest extends TestCase
|
||||
$ret = json_decode((string) $response->getBody(), true);
|
||||
$this->assertArrayHasKey('error', $ret);
|
||||
$this->assertArraySubset([
|
||||
'code' => -32603,
|
||||
'message' => 'Internal error.',
|
||||
'code' => ResponseBuilder::SERVER_ERROR,
|
||||
'message' => 'Expected non-zero value of divider',
|
||||
], $ret['error']);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user