20
20
use Hyperf \Rpc \Protocol ;
21
21
use Hyperf \Rpc \Response as RPCResponse ;
22
22
use Hyperf \RpcMultiplex \Contract \HttpMessageBuilderInterface ;
23
- use InvalidArgumentException ;
23
+ use Hyperf \RpcMultiplex \Exception \InvalidArgumentException ;
24
+ use Hyperf \RpcMultiplex \Exception \NotFoundException ;
24
25
use Psr \Container \ContainerInterface ;
25
26
use Psr \Http \Message \ServerRequestInterface ;
26
27
use Swow \Psr7 \Message \ResponsePlusInterface ;
@@ -42,35 +43,38 @@ public function __construct(ContainerInterface $container, Protocol $protocol, H
42
43
43
44
protected function handleFound (Dispatched $ dispatched , ServerRequestInterface $ request ): mixed
44
45
{
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
+ }
63
62
64
- try {
65
63
$ 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 ;
72
64
}
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 ;
73
76
}
77
+
74
78
return $ this ->buildData ($ request , $ response );
75
79
}
76
80
0 commit comments