diff --git a/quobyte/rpc_client.go b/quobyte/rpc_client.go index 552b3b7..a412cda 100644 --- a/quobyte/rpc_client.go +++ b/quobyte/rpc_client.go @@ -15,7 +15,8 @@ import ( ) const ( - emptyResponse string = "Empty result and no error occurred" + emptyResponse string = "Empty result and no error occurred" + errorMessageFormat string = "method: %s error message: %s" ) var mux sync.Mutex @@ -64,7 +65,7 @@ func encodeRequest(method string, params interface{}) ([]byte, error) { }) } -func decodeResponse(ioReader io.Reader, reply interface{}) error { +func decodeResponse(method string, ioReader io.Reader, reply interface{}) error { var resp response if err := json.NewDecoder(ioReader).Decode(&resp); err != nil { return err @@ -77,12 +78,12 @@ func decodeResponse(ioReader io.Reader, reply interface{}) error { } if rpcErr.Message != "" { - return errors.New(rpcErr.Message) + return fmt.Errorf(errorMessageFormat, method, rpcErr.Message) } respError := rpcErr.decodeErrorCode() if respError != "" { - return errors.New(respError) + return fmt.Errorf(errorMessageFormat, method, respError) } } @@ -90,7 +91,7 @@ func decodeResponse(ioReader io.Reader, reply interface{}) error { return json.Unmarshal(*resp.Result, reply) } - return errors.New(emptyResponse) + return fmt.Errorf(errorMessageFormat, method, emptyResponse) } func (client QuobyteClient) sendRequest(method string, request interface{}, response interface{}) error { @@ -157,6 +158,6 @@ func (client QuobyteClient) sendRequest(method string, request interface{}, resp return fmt.Errorf("JsonRPC failed with error (error code: %d) %s", resp.StatusCode, string(body)) } - return decodeResponse(resp.Body, &response) + return decodeResponse(method, resp.Body, &response) } } diff --git a/quobyte/rpc_client_test.go b/quobyte/rpc_client_test.go index 429cf28..92fed48 100644 --- a/quobyte/rpc_client_test.go +++ b/quobyte/rpc_client_test.go @@ -3,6 +3,7 @@ package quobyte import ( "bytes" "encoding/json" + "fmt" "net/http" "net/http/httptest" "reflect" @@ -62,7 +63,7 @@ func TestSuccessfullDecodeResponse(t *testing.T) { res, _ := json.Marshal(expectedResult) var resp CreateVolumeResponse - err := decodeResponse(bytes.NewReader(res), &resp) + err := decodeResponse("", bytes.NewReader(res), &resp) if err != nil { t.Log(err) t.Fail() @@ -75,7 +76,8 @@ func TestSuccessfullDecodeResponse(t *testing.T) { } func TestSuccessfullDecodeResponseWithErrorMessage(t *testing.T) { - errorMessage := "ERROR_CODE_INVALID_REQUEST" + method := "testMethod" + errorMessage := fmt.Sprintf(errorMessageFormat, method, "ERROR_CODE_INVALID_REQUEST") var byt json.RawMessage byt, _ = json.Marshal(&rpcError{ Code: -32600, @@ -91,7 +93,7 @@ func TestSuccessfullDecodeResponseWithErrorMessage(t *testing.T) { res, _ := json.Marshal(expectedResult) var resp CreateVolumeResponse - err := decodeResponse(bytes.NewReader(res), &resp) + err := decodeResponse(method, bytes.NewReader(res), &resp) if err == nil { t.Log("No error occurred") t.Fail() @@ -104,7 +106,8 @@ func TestSuccessfullDecodeResponseWithErrorMessage(t *testing.T) { } func TestSuccesfullDecodeResponseWithErrorCode(t *testing.T) { - errorMessage := "ERROR_CODE_INVALID_REQUEST" + method := "testMethod" + errorMessage := fmt.Sprintf(errorMessageFormat, method, "ERROR_CODE_INVALID_REQUEST") var byt json.RawMessage byt, _ = json.Marshal(&rpcError{ Code: -32600, @@ -119,7 +122,7 @@ func TestSuccesfullDecodeResponseWithErrorCode(t *testing.T) { res, _ := json.Marshal(expectedResult) var resp CreateVolumeResponse - err := decodeResponse(bytes.NewReader(res), &resp) + err := decodeResponse(method, bytes.NewReader(res), &resp) if err == nil { t.Log("No error occurred") t.Fail() @@ -132,6 +135,8 @@ func TestSuccesfullDecodeResponseWithErrorCode(t *testing.T) { } func TestBadDecodeResponse(t *testing.T) { + method := "testMethod" + expectedError := fmt.Sprintf(errorMessageFormat, method, emptyResponse) expectedResult := &response{ ID: "0", Version: "2.0", @@ -140,14 +145,14 @@ func TestBadDecodeResponse(t *testing.T) { res, _ := json.Marshal(expectedResult) var resp CreateVolumeResponse - err := decodeResponse(bytes.NewReader(res), &resp) + err := decodeResponse(method, bytes.NewReader(res), &resp) if err == nil { t.Log("No error occurred") t.Fail() } - if emptyResponse != err.Error() { - t.Logf("Expected: %s got %s\n", emptyResponse, err.Error()) + if expectedError != err.Error() { + t.Logf("Expected: %s got %s\n", expectedError, err.Error()) t.Fail() } }