-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathrpc_client.go
More file actions
42 lines (34 loc) · 933 Bytes
/
rpc_client.go
File metadata and controls
42 lines (34 loc) · 933 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package node
import (
"context"
"github.com/0gfoundation/0g-storage-client/common/rpc"
providers "github.com/openweb3/go-rpc-provider/provider_wrapper"
)
type rpcClient struct {
*rpc.Client
}
func newRpcClient(url string, option ...providers.Option) (*rpcClient, error) {
inner, err := rpc.NewClient(url, option...)
if err != nil {
return nil, err
}
client := rpcClient{inner}
client.HookCallContext(client.rpcErrorMiddleware)
return &client, nil
}
func (c *rpcClient) wrapError(e error, method string) error {
if e == nil {
return nil
}
return &RPCError{
Message: e.Error(),
Method: method,
URL: c.URL(),
}
}
func (c *rpcClient) rpcErrorMiddleware(handler providers.CallContextFunc) providers.CallContextFunc {
return func(ctx context.Context, result interface{}, method string, args ...interface{}) error {
err := handler(ctx, result, method, args...)
return c.wrapError(err, method)
}
}