Skip to content

Commit 624ad55

Browse files
authored
wrap errors using %w (#52)
1 parent 67254f4 commit 624ad55

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

jsonrpc.go

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,25 @@ type RPCClient interface {
135135
// Params() is a helper function that uses the same parameter syntax as Call().
136136
//
137137
// e.g. to manually create an RPCRequest object:
138-
// request := &RPCRequest{
139-
// Method: "myMethod",
140-
// Params: Params("Alex", 35, true),
141-
// }
138+
//
139+
// request := &RPCRequest{
140+
// Method: "myMethod",
141+
// Params: Params("Alex", 35, true),
142+
// }
142143
//
143144
// If you know what you are doing you can omit the Params() call to avoid some reflection but potentially create incorrect rpc requests:
144-
//request := &RPCRequest{
145-
// Method: "myMethod",
146-
// Params: 2, <-- invalid since a single primitive value must be wrapped in an array --> no magic without Params()
147-
// }
145+
//
146+
// request := &RPCRequest{
147+
// Method: "myMethod",
148+
// Params: 2, <-- invalid since a single primitive value must be wrapped in an array --> no magic without Params()
149+
// }
148150
//
149151
// correct:
150-
// request := &RPCRequest{
151-
// Method: "myMethod",
152-
// Params: []int{2}, <-- invalid since a single primitive value must be wrapped in an array
153-
// }
152+
//
153+
// request := &RPCRequest{
154+
// Method: "myMethod",
155+
// Params: []int{2}, <-- invalid since a single primitive value must be wrapped in an array
156+
// }
154157
type RPCRequest struct {
155158
Method string `json:"method"`
156159
Params interface{} `json:"params,omitempty"`
@@ -427,11 +430,11 @@ func (client *rpcClient) doCall(ctx context.Context, RPCRequest *RPCRequest) (*R
427430

428431
httpRequest, err := client.newRequest(ctx, RPCRequest)
429432
if err != nil {
430-
return nil, fmt.Errorf("rpc call %v() on %v: %v", RPCRequest.Method, client.endpoint, err.Error())
433+
return nil, fmt.Errorf("rpc call %v() on %v: %w", RPCRequest.Method, client.endpoint, err)
431434
}
432435
httpResponse, err := client.httpClient.Do(httpRequest)
433436
if err != nil {
434-
return nil, fmt.Errorf("rpc call %v() on %v: %v", RPCRequest.Method, httpRequest.URL.String(), err.Error())
437+
return nil, fmt.Errorf("rpc call %v() on %v: %w", RPCRequest.Method, httpRequest.URL.String(), err)
435438
}
436439
defer httpResponse.Body.Close()
437440

@@ -449,10 +452,10 @@ func (client *rpcClient) doCall(ctx context.Context, RPCRequest *RPCRequest) (*R
449452
if httpResponse.StatusCode >= 400 {
450453
return nil, &HTTPError{
451454
Code: httpResponse.StatusCode,
452-
err: fmt.Errorf("rpc call %v() on %v status code: %v. could not decode body to rpc response: %v", RPCRequest.Method, httpRequest.URL.String(), httpResponse.StatusCode, err.Error()),
455+
err: fmt.Errorf("rpc call %v() on %v status code: %v. could not decode body to rpc response: %w", RPCRequest.Method, httpRequest.URL.String(), httpResponse.StatusCode, err),
453456
}
454457
}
455-
return nil, fmt.Errorf("rpc call %v() on %v status code: %v. could not decode body to rpc response: %v", RPCRequest.Method, httpRequest.URL.String(), httpResponse.StatusCode, err.Error())
458+
return nil, fmt.Errorf("rpc call %v() on %v status code: %v. could not decode body to rpc response: %w", RPCRequest.Method, httpRequest.URL.String(), httpResponse.StatusCode, err)
456459
}
457460

458461
// response body empty
@@ -487,11 +490,11 @@ func (client *rpcClient) doCall(ctx context.Context, RPCRequest *RPCRequest) (*R
487490
func (client *rpcClient) doBatchCall(ctx context.Context, rpcRequest []*RPCRequest) ([]*RPCResponse, error) {
488491
httpRequest, err := client.newRequest(ctx, rpcRequest)
489492
if err != nil {
490-
return nil, fmt.Errorf("rpc batch call on %v: %v", client.endpoint, err.Error())
493+
return nil, fmt.Errorf("rpc batch call on %v: %w", client.endpoint, err)
491494
}
492495
httpResponse, err := client.httpClient.Do(httpRequest)
493496
if err != nil {
494-
return nil, fmt.Errorf("rpc batch call on %v: %v", httpRequest.URL.String(), err.Error())
497+
return nil, fmt.Errorf("rpc batch call on %v: %w", httpRequest.URL.String(), err)
495498
}
496499
defer httpResponse.Body.Close()
497500

@@ -509,10 +512,10 @@ func (client *rpcClient) doBatchCall(ctx context.Context, rpcRequest []*RPCReque
509512
if httpResponse.StatusCode >= 400 {
510513
return nil, &HTTPError{
511514
Code: httpResponse.StatusCode,
512-
err: fmt.Errorf("rpc batch call on %v status code: %v. could not decode body to rpc response: %v", httpRequest.URL.String(), httpResponse.StatusCode, err.Error()),
515+
err: fmt.Errorf("rpc batch call on %v status code: %v. could not decode body to rpc response: %w", httpRequest.URL.String(), httpResponse.StatusCode, err),
513516
}
514517
}
515-
return nil, fmt.Errorf("rpc batch call on %v status code: %v. could not decode body to rpc response: %v", httpRequest.URL.String(), httpResponse.StatusCode, err.Error())
518+
return nil, fmt.Errorf("rpc batch call on %v status code: %v. could not decode body to rpc response: %w", httpRequest.URL.String(), httpResponse.StatusCode, err)
516519
}
517520

518521
// response body empty
@@ -542,25 +545,28 @@ func (client *rpcClient) doBatchCall(ctx context.Context, rpcRequest []*RPCReque
542545
// But you should consider to always use NewRequest() instead.
543546
//
544547
// e.g. to manually create an RPCRequest object:
545-
// request := &RPCRequest{
546-
// Method: "myMethod",
547-
// Params: Params("Alex", 35, true),
548-
// }
548+
//
549+
// request := &RPCRequest{
550+
// Method: "myMethod",
551+
// Params: Params("Alex", 35, true),
552+
// }
549553
//
550554
// same with new request:
551555
// request := NewRequest("myMethod", "Alex", 35, true)
552556
//
553557
// If you know what you are doing you can omit the Params() call but potentially create incorrect rpc requests:
554-
// request := &RPCRequest{
555-
// Method: "myMethod",
556-
// Params: 2, <-- invalid since a single primitive value must be wrapped in an array --> no magic without Params()
557-
// }
558+
//
559+
// request := &RPCRequest{
560+
// Method: "myMethod",
561+
// Params: 2, <-- invalid since a single primitive value must be wrapped in an array --> no magic without Params()
562+
// }
558563
//
559564
// correct:
560-
// request := &RPCRequest{
561-
// Method: "myMethod",
562-
// Params: []int{2}, <-- valid since a single primitive value must be wrapped in an array
563-
// }
565+
//
566+
// request := &RPCRequest{
567+
// Method: "myMethod",
568+
// Params: []int{2}, <-- valid since a single primitive value must be wrapped in an array
569+
// }
564570
func Params(params ...interface{}) interface{} {
565571
var finalParams interface{}
566572

0 commit comments

Comments
 (0)