13
13
14
14
use Hyperf \Rpc \Context ;
15
15
use Hyperf \Rpc \Contract \DataFormatterInterface ;
16
+ use Hyperf \Rpc \ErrorResponse ;
17
+ use Hyperf \Rpc \Request ;
18
+ use Hyperf \Rpc \Response ;
16
19
use Hyperf \RpcClient \Exception \RequestException ;
17
20
use Hyperf \RpcMultiplex \Contract \DataFetcherInterface ;
18
21
use Hyperf \Utils \Codec \Json ;
@@ -26,44 +29,41 @@ public function __construct(Context $context)
26
29
$ this ->context = $ context ;
27
30
}
28
31
29
- public function formatRequest (array $ data ): array
32
+ public function formatRequest (Request $ request ): array
30
33
{
31
- [$ path , $ params , $ id ] = $ data ;
32
34
return [
33
- Constant::ID => $ id ,
34
- Constant::PATH => $ path ,
35
- Constant::DATA => $ params ,
35
+ Constant::ID => $ request -> getId () ,
36
+ Constant::PATH => $ request -> getPath () ,
37
+ Constant::DATA => $ request -> getParams () ,
36
38
Constant::CONTEXT => $ this ->context ->getData (),
37
39
];
38
40
}
39
41
40
- public function formatResponse (array $ data ): array
42
+ public function formatResponse (Response $ response ): array
41
43
{
42
- [$ id , $ result ] = $ data ;
43
44
return [
44
- Constant::ID => $ id ,
45
- Constant::RESULT => $ result ,
45
+ Constant::ID => $ response -> getId () ,
46
+ Constant::RESULT => $ response -> getResult () ,
46
47
Constant::CONTEXT => $ this ->context ->getData (),
47
48
];
48
49
}
49
50
50
- public function formatErrorResponse (array $ data ): array
51
+ public function formatErrorResponse (ErrorResponse $ response ): array
51
52
{
52
- [$ id , $ code , $ message , $ data ] = $ data ;
53
-
54
- if (isset ($ data ) && $ data instanceof \Throwable) {
55
- $ data = [
56
- 'class ' => get_class ($ data ),
57
- 'code ' => $ data ->getCode (),
58
- 'message ' => $ data ->getMessage (),
53
+ $ exception = $ response ->getException ();
54
+ if ($ exception instanceof \Throwable) {
55
+ $ exception = [
56
+ 'class ' => get_class ($ exception ),
57
+ 'code ' => $ exception ->getCode (),
58
+ 'message ' => $ exception ->getMessage (),
59
59
];
60
60
}
61
61
return [
62
- Constant::ID => $ id ?? null ,
62
+ Constant::ID => $ response -> getId () ,
63
63
Constant::ERROR => [
64
- Constant::CODE => $ code ,
65
- Constant::MESSAGE => $ message ,
66
- Constant::DATA => $ data ,
64
+ Constant::CODE => $ response -> getCode () ,
65
+ Constant::MESSAGE => $ response -> getMessage () ,
66
+ Constant::DATA => $ exception ,
67
67
],
68
68
Constant::CONTEXT => $ this ->context ->getData (),
69
69
];
0 commit comments