Skip to content

Commit df5f2e9

Browse files
Added error handling when using callback in multiplexed RPC.
Co-authored-by: 李铭昕 <715557344@qq.com>
1 parent b544c70 commit df5f2e9

File tree

2 files changed

+49
-26
lines changed

2 files changed

+49
-26
lines changed

src/CoreMiddleware.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
use Hyperf\Rpc\Protocol;
2121
use Hyperf\Rpc\Response as RPCResponse;
2222
use Hyperf\RpcMultiplex\Contract\HttpMessageBuilderInterface;
23-
use InvalidArgumentException;
23+
use Hyperf\RpcMultiplex\Exception\InvalidArgumentException;
24+
use Hyperf\RpcMultiplex\Exception\NotFoundException;
2425
use Psr\Container\ContainerInterface;
2526
use Psr\Http\Message\ServerRequestInterface;
2627
use Swow\Psr7\Message\ResponsePlusInterface;
@@ -42,35 +43,38 @@ public function __construct(ContainerInterface $container, Protocol $protocol, H
4243

4344
protected function handleFound(Dispatched $dispatched, ServerRequestInterface $request): mixed
4445
{
45-
if ($dispatched->handler->callback instanceof Closure) {
46-
$callback = $dispatched->handler->callback;
47-
$response = $callback();
48-
} else {
49-
[$controller, $action] = $this->prepareHandler($dispatched->handler->callback);
50-
$controllerInstance = $this->container->get($controller);
51-
if (! method_exists($controller, $action)) {
52-
// Route found, but the handler does not exist.
53-
$data = $this->buildErrorData($request, 500, 'The handler does not exists.');
54-
return $this->responseBuilder->buildResponse($request, $data);
55-
}
56-
57-
try {
58-
$parameters = $this->parseMethodParameters($controller, $action, $request->getParsedBody());
59-
} catch (InvalidArgumentException $exception) {
60-
$data = $this->buildErrorData($request, 400, 'The params is invalid.', $exception);
61-
return $this->responseBuilder->buildResponse($request, $data);
62-
}
46+
try {
47+
if ($dispatched->handler->callback instanceof Closure) {
48+
$callback = $dispatched->handler->callback;
49+
$response = $callback();
50+
} else {
51+
[$controller, $action] = $this->prepareHandler($dispatched->handler->callback);
52+
$controllerInstance = $this->container->get($controller);
53+
if (! method_exists($controller, $action)) {
54+
throw new NotFoundException('The handler does not exists.');
55+
}
56+
57+
try {
58+
$parameters = $this->parseMethodParameters($controller, $action, $request->getParsedBody());
59+
} catch (\InvalidArgumentException) {
60+
throw new InvalidArgumentException('The params is invalid.');
61+
}
6362

64-
try {
6563
$response = $controllerInstance->{$action}(...$parameters);
66-
} catch (Throwable $exception) {
67-
$data = $this->buildErrorData($request, 500, $exception->getMessage(), $exception);
68-
$response = $this->responseBuilder->buildResponse($request, $data);
69-
$this->responseBuilder->persistToContext($response);
70-
71-
throw $exception;
7264
}
65+
} catch (NotFoundException $exception) {
66+
$data = $this->buildErrorData($request, 500, $exception->getMessage(), $exception);
67+
return $this->responseBuilder->buildResponse($request, $data);
68+
} catch (InvalidArgumentException $exception) {
69+
$data = $this->buildErrorData($request, 400, $exception->getMessage(), $exception);
70+
return $this->responseBuilder->buildResponse($request, $data);
71+
} catch (Throwable $exception) {
72+
$data = $this->buildErrorData($request, 500, $exception->getMessage(), $exception);
73+
$response = $this->responseBuilder->buildResponse($request, $data);
74+
$this->responseBuilder->persistToContext($response);
75+
throw $exception;
7376
}
77+
7478
return $this->buildData($request, $response);
7579
}
7680

src/Exception/NotFoundException.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace Hyperf\RpcMultiplex\Exception;
14+
15+
use RuntimeException;
16+
17+
class NotFoundException extends RuntimeException
18+
{
19+
}

0 commit comments

Comments
 (0)