Skip to content

Commit 3786a63

Browse files
committed
Add failing RPC method to api error response
This makes debugging api clients such as CSI debugging relatively easy.
1 parent f120dc0 commit 3786a63

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

quobyte/rpc_client.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import (
1515
)
1616

1717
const (
18-
emptyResponse string = "Empty result and no error occurred"
18+
emptyResponse string = "Empty result and no error occurred"
19+
errorMessageFormat string = "method: %s error message: %s"
1920
)
2021

2122
var mux sync.Mutex
@@ -64,7 +65,7 @@ func encodeRequest(method string, params interface{}) ([]byte, error) {
6465
})
6566
}
6667

67-
func decodeResponse(ioReader io.Reader, reply interface{}) error {
68+
func decodeResponse(method string, ioReader io.Reader, reply interface{}) error {
6869
var resp response
6970
if err := json.NewDecoder(ioReader).Decode(&resp); err != nil {
7071
return err
@@ -77,20 +78,20 @@ func decodeResponse(ioReader io.Reader, reply interface{}) error {
7778
}
7879

7980
if rpcErr.Message != "" {
80-
return errors.New(rpcErr.Message)
81+
return fmt.Errorf(errorMessageFormat, method, rpcErr.Message)
8182
}
8283

8384
respError := rpcErr.decodeErrorCode()
8485
if respError != "" {
85-
return errors.New(respError)
86+
return fmt.Errorf(errorMessageFormat, method, respError)
8687
}
8788
}
8889

8990
if resp.Result != nil && reply != nil {
9091
return json.Unmarshal(*resp.Result, reply)
9192
}
9293

93-
return errors.New(emptyResponse)
94+
return fmt.Errorf(errorMessageFormat, method, emptyResponse)
9495
}
9596

9697
func (client QuobyteClient) sendRequest(method string, request interface{}, response interface{}) error {
@@ -157,6 +158,6 @@ func (client QuobyteClient) sendRequest(method string, request interface{}, resp
157158
return fmt.Errorf("JsonRPC failed with error (error code: %d) %s",
158159
resp.StatusCode, string(body))
159160
}
160-
return decodeResponse(resp.Body, &response)
161+
return decodeResponse(method, resp.Body, &response)
161162
}
162163
}

quobyte/rpc_client_test.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package quobyte
33
import (
44
"bytes"
55
"encoding/json"
6+
"fmt"
67
"net/http"
78
"net/http/httptest"
89
"reflect"
@@ -62,7 +63,7 @@ func TestSuccessfullDecodeResponse(t *testing.T) {
6263
res, _ := json.Marshal(expectedResult)
6364

6465
var resp CreateVolumeResponse
65-
err := decodeResponse(bytes.NewReader(res), &resp)
66+
err := decodeResponse("", bytes.NewReader(res), &resp)
6667
if err != nil {
6768
t.Log(err)
6869
t.Fail()
@@ -75,7 +76,8 @@ func TestSuccessfullDecodeResponse(t *testing.T) {
7576
}
7677

7778
func TestSuccessfullDecodeResponseWithErrorMessage(t *testing.T) {
78-
errorMessage := "ERROR_CODE_INVALID_REQUEST"
79+
method := "testMethod"
80+
errorMessage := fmt.Sprintf(errorMessageFormat, method, "ERROR_CODE_INVALID_REQUEST")
7981
var byt json.RawMessage
8082
byt, _ = json.Marshal(&rpcError{
8183
Code: -32600,
@@ -91,7 +93,7 @@ func TestSuccessfullDecodeResponseWithErrorMessage(t *testing.T) {
9193
res, _ := json.Marshal(expectedResult)
9294

9395
var resp CreateVolumeResponse
94-
err := decodeResponse(bytes.NewReader(res), &resp)
96+
err := decodeResponse(method, bytes.NewReader(res), &resp)
9597
if err == nil {
9698
t.Log("No error occurred")
9799
t.Fail()
@@ -104,7 +106,8 @@ func TestSuccessfullDecodeResponseWithErrorMessage(t *testing.T) {
104106
}
105107

106108
func TestSuccesfullDecodeResponseWithErrorCode(t *testing.T) {
107-
errorMessage := "ERROR_CODE_INVALID_REQUEST"
109+
method := "testMethod"
110+
errorMessage := fmt.Sprintf(errorMessageFormat, method, "ERROR_CODE_INVALID_REQUEST")
108111
var byt json.RawMessage
109112
byt, _ = json.Marshal(&rpcError{
110113
Code: -32600,
@@ -119,7 +122,7 @@ func TestSuccesfullDecodeResponseWithErrorCode(t *testing.T) {
119122
res, _ := json.Marshal(expectedResult)
120123

121124
var resp CreateVolumeResponse
122-
err := decodeResponse(bytes.NewReader(res), &resp)
125+
err := decodeResponse(method, bytes.NewReader(res), &resp)
123126
if err == nil {
124127
t.Log("No error occurred")
125128
t.Fail()
@@ -132,6 +135,8 @@ func TestSuccesfullDecodeResponseWithErrorCode(t *testing.T) {
132135
}
133136

134137
func TestBadDecodeResponse(t *testing.T) {
138+
method := "testMethod"
139+
expectedError := fmt.Sprintf(errorMessageFormat, method, emptyResponse)
135140
expectedResult := &response{
136141
ID: "0",
137142
Version: "2.0",
@@ -140,14 +145,14 @@ func TestBadDecodeResponse(t *testing.T) {
140145
res, _ := json.Marshal(expectedResult)
141146

142147
var resp CreateVolumeResponse
143-
err := decodeResponse(bytes.NewReader(res), &resp)
148+
err := decodeResponse(method, bytes.NewReader(res), &resp)
144149
if err == nil {
145150
t.Log("No error occurred")
146151
t.Fail()
147152
}
148153

149-
if emptyResponse != err.Error() {
150-
t.Logf("Expected: %s got %s\n", emptyResponse, err.Error())
154+
if expectedError != err.Error() {
155+
t.Logf("Expected: %s got %s\n", expectedError, err.Error())
151156
t.Fail()
152157
}
153158
}

0 commit comments

Comments
 (0)