Skip to content

Commit

Permalink
Add failing RPC method to api error response
Browse files Browse the repository at this point in the history
This makes debugging api clients such as CSI
debugging relatively easy.
  • Loading branch information
venkatsc committed Mar 3, 2022
1 parent f120dc0 commit 3786a63
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
13 changes: 7 additions & 6 deletions quobyte/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -77,20 +78,20 @@ 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)
}
}

if resp.Result != nil && reply != nil {
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 {
Expand Down Expand Up @@ -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)
}
}
21 changes: 13 additions & 8 deletions quobyte/rpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package quobyte
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand All @@ -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()
Expand All @@ -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,
Expand All @@ -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()
Expand All @@ -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",
Expand All @@ -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()
}
}
Expand Down

0 comments on commit 3786a63

Please sign in to comment.