Added error handling when using callback in multiplexed RPC.

Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
Binary Alan 2024-04-11 10:44:55 +08:00 committed by GitHub
parent 05129254e5
commit c139faa508
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 53 additions and 26 deletions

View File

@ -4,12 +4,16 @@
- [#6664](https://github.com/hyperf/hyperf/pull/6664) Fixed bug that `isset` cannot check `null` in `Hyperf\Collection\Collection`.
## Optimized
- [#6668](https://github.com/hyperf/hyperf/pull/6668) Added error handling when using `callback` in multiplexed RPC.
# v3.1.17 - 2024-04-10
## Added
- [#6652](https://github.com/hyperf/hyperf/pull/6652) Added Str trim methods.
- [#6658](https://github.com/hyperf/hyperf/pull/6658) HEAD requests, attempt fallback to GET in `MiddlewareManager`
- [#6658](https://github.com/hyperf/hyperf/pull/6658) HEAD requests, attempt fallback to GET in `MiddlewareManager`.
- [#6665](https://github.com/hyperf/hyperf/pull/6665) Added logger for `Websocket`.
# Changed

View File

@ -20,7 +20,8 @@ use Hyperf\Rpc\ErrorResponse;
use Hyperf\Rpc\Protocol;
use Hyperf\Rpc\Response as RPCResponse;
use Hyperf\RpcMultiplex\Contract\HttpMessageBuilderInterface;
use InvalidArgumentException;
use Hyperf\RpcMultiplex\Exception\InvalidArgumentException;
use Hyperf\RpcMultiplex\Exception\NotFoundException;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Swow\Psr7\Message\ResponsePlusInterface;
@ -42,35 +43,38 @@ class CoreMiddleware extends \Hyperf\RpcServer\CoreMiddleware
protected function handleFound(Dispatched $dispatched, ServerRequestInterface $request): mixed
{
if ($dispatched->handler->callback instanceof Closure) {
$callback = $dispatched->handler->callback;
$response = $callback();
} else {
[$controller, $action] = $this->prepareHandler($dispatched->handler->callback);
$controllerInstance = $this->container->get($controller);
if (! method_exists($controller, $action)) {
// Route found, but the handler does not exist.
$data = $this->buildErrorData($request, 500, 'The handler does not exists.');
return $this->responseBuilder->buildResponse($request, $data);
}
try {
if ($dispatched->handler->callback instanceof Closure) {
$callback = $dispatched->handler->callback;
$response = $callback();
} else {
[$controller, $action] = $this->prepareHandler($dispatched->handler->callback);
$controllerInstance = $this->container->get($controller);
if (! method_exists($controller, $action)) {
throw new NotFoundException('The handler does not exists.');
}
try {
$parameters = $this->parseMethodParameters($controller, $action, $request->getParsedBody());
} catch (InvalidArgumentException $exception) {
$data = $this->buildErrorData($request, 400, 'The params is invalid.', $exception);
return $this->responseBuilder->buildResponse($request, $data);
}
try {
$parameters = $this->parseMethodParameters($controller, $action, $request->getParsedBody());
} catch (\InvalidArgumentException) {
throw new InvalidArgumentException('The params is invalid.');
}
try {
$response = $controllerInstance->{$action}(...$parameters);
} catch (Throwable $exception) {
$data = $this->buildErrorData($request, 500, $exception->getMessage(), $exception);
$response = $this->responseBuilder->buildResponse($request, $data);
$this->responseBuilder->persistToContext($response);
throw $exception;
}
} catch (NotFoundException $exception) {
$data = $this->buildErrorData($request, 500, $exception->getMessage(), $exception);
return $this->responseBuilder->buildResponse($request, $data);
} catch (InvalidArgumentException $exception) {
$data = $this->buildErrorData($request, 400, $exception->getMessage(), $exception);
return $this->responseBuilder->buildResponse($request, $data);
} catch (Throwable $exception) {
$data = $this->buildErrorData($request, 500, $exception->getMessage(), $exception);
$response = $this->responseBuilder->buildResponse($request, $data);
$this->responseBuilder->persistToContext($response);
throw $exception;
}
return $this->buildData($request, $response);
}

View File

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\RpcMultiplex\Exception;
use RuntimeException;
class NotFoundException extends RuntimeException
{
}