Skip to content

Commit 3dabc3d

Browse files
authored
Merge pull request #72 from filecoin-project/71-create-discernible-error-types-for-rpc-connection-errors
Add RPCConnectionError and wrap rpc client connection errors with it
2 parents 9918975 + e1b0cbb commit 3dabc3d

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (e *ErrClient) Error() string {
6060
}
6161

6262
// Unwrap unwraps the actual error
63-
func (e *ErrClient) Unwrap(err error) error {
63+
func (e *ErrClient) Unwrap() error {
6464
return e.err
6565
}
6666

@@ -149,7 +149,7 @@ func httpClient(ctx context.Context, addr string, namespace string, outs []inter
149149

150150
hreq, err := http.NewRequest("POST", addr, bytes.NewReader(b))
151151
if err != nil {
152-
return clientResponse{}, err
152+
return clientResponse{}, &RPCConnectionError{err}
153153
}
154154

155155
hreq.Header = requestHeader.Clone()
@@ -162,7 +162,7 @@ func httpClient(ctx context.Context, addr string, namespace string, outs []inter
162162

163163
httpResp, err := _defaultHTTPClient.Do(hreq)
164164
if err != nil {
165-
return clientResponse{}, err
165+
return clientResponse{}, &RPCConnectionError{err}
166166
}
167167
defer httpResp.Body.Close()
168168

@@ -192,7 +192,7 @@ func websocketClient(ctx context.Context, addr string, namespace string, outs []
192192
connFactory := func() (*websocket.Conn, error) {
193193
conn, _, err := websocket.DefaultDialer.Dial(addr, requestHeader)
194194
if err != nil {
195-
return nil, xerrors.Errorf("cannot dial address %s for %w", addr, err)
195+
return nil, &RPCConnectionError{xerrors.Errorf("cannot dial address %s for %w", addr, err)}
196196
}
197197
return conn, nil
198198
}

errors.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ import (
55
"reflect"
66
)
77

8+
const eTempWSError = -1111111
9+
10+
type RPCConnectionError struct {
11+
err error
12+
}
13+
14+
func (e *RPCConnectionError) Error() string {
15+
return e.err.Error()
16+
}
17+
18+
func (e *RPCConnectionError) Unwrap() error {
19+
return e.err
20+
}
21+
822
type Errors struct {
923
byType map[reflect.Type]ErrorCode
1024
byCode map[ErrorCode]reflect.Type
@@ -17,7 +31,9 @@ const FirstUserCode = 2
1731
func NewErrors() Errors {
1832
return Errors{
1933
byType: map[reflect.Type]ErrorCode{},
20-
byCode: map[ErrorCode]reflect.Type{},
34+
byCode: map[ErrorCode]reflect.Type{
35+
-1111111: reflect.TypeOf(RPCConnectionError{}),
36+
},
2137
}
2238
}
2339

rpc_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,34 @@ func (h *SimpleServerHandler) ErrChanSub(ctx context.Context) (<-chan int, error
131131
return nil, errors.New("expect to return an error")
132132
}
133133

134+
func TestRPCBadConnection(t *testing.T) {
135+
// setup server
136+
137+
serverHandler := &SimpleServerHandler{}
138+
139+
rpcServer := NewServer()
140+
rpcServer.Register("SimpleServerHandler", serverHandler)
141+
142+
// httptest stuff
143+
testServ := httptest.NewServer(rpcServer)
144+
defer testServ.Close()
145+
// setup client
146+
147+
var client struct {
148+
Add func(int) error
149+
AddGet func(int) int
150+
StringMatch func(t TestType, i2 int64) (out TestOut, err error)
151+
ErrChanSub func(context.Context) (<-chan int, error)
152+
}
153+
closer, err := NewClient(context.Background(), "http://"+testServ.Listener.Addr().String()+"0", "SimpleServerHandler", &client, nil)
154+
require.NoError(t, err)
155+
err = client.Add(2)
156+
require.True(t, errors.As(err, new(*RPCConnectionError)))
157+
158+
defer closer()
159+
160+
}
161+
134162
func TestRPC(t *testing.T) {
135163
// setup server
136164

websocket.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ const wsCancel = "xrpc.cancel"
1919
const chValue = "xrpc.ch.val"
2020
const chClose = "xrpc.ch.close"
2121

22-
const eTempWSError = -1111111
23-
2422
type frame struct {
2523
// common
2624
Jsonrpc string `json:"jsonrpc"`

0 commit comments

Comments
 (0)