Skip to content

Commit

Permalink
fix: improve error paths in BaseService.Request()
Browse files Browse the repository at this point in the history
  • Loading branch information
padamstx committed Jul 29, 2020
1 parent 3cc1325 commit c5dd77f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
9 changes: 5 additions & 4 deletions v4/core/base_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func (service *BaseService) Request(req *http.Request, result interface{}) (deta

err = service.Options.Authenticator.Authenticate(req)
if err != nil {
err = fmt.Errorf(ERRORMSG_AUTHENTICATE_ERROR, err.Error())
return
}

Expand Down Expand Up @@ -254,7 +255,7 @@ func (service *BaseService) Request(req *http.Request, result interface{}) (deta
defer httpResponse.Body.Close()
responseBody, readErr = ioutil.ReadAll(httpResponse.Body)
if readErr != nil {
err = fmt.Errorf("An error occurred while reading the response body: '%s'", readErr.Error())
err = fmt.Errorf(ERRORMSG_READ_RESPONSE_BODY, readErr.Error())
return
}
}
Expand Down Expand Up @@ -301,7 +302,7 @@ func (service *BaseService) Request(req *http.Request, result interface{}) (deta
defer httpResponse.Body.Close()
responseBody, readErr := ioutil.ReadAll(httpResponse.Body)
if readErr != nil {
err = fmt.Errorf("An error occurred while reading the response body: '%s'", readErr.Error())
err = fmt.Errorf(ERRORMSG_READ_RESPONSE_BODY, readErr.Error())
return
}

Expand All @@ -310,7 +311,7 @@ func (service *BaseService) Request(req *http.Request, result interface{}) (deta
if decodeErr != nil {
// Error decoding the response body.
// Return the response body in RawResult, along with an error.
err = fmt.Errorf("An error occurred while unmarshalling the response body: '%s'", decodeErr.Error())
err = fmt.Errorf(ERRORMSG_UNMARSHAL_RESPONSE_BODY, decodeErr.Error())
detailedResponse.RawResult = responseBody
return
}
Expand All @@ -325,7 +326,7 @@ func (service *BaseService) Request(req *http.Request, result interface{}) (deta
defer httpResponse.Body.Close()
responseBody, readErr := ioutil.ReadAll(httpResponse.Body)
if readErr != nil {
err = fmt.Errorf("An error occurred while reading the response body: '%s'", readErr.Error())
err = fmt.Errorf(ERRORMSG_READ_RESPONSE_BODY, readErr.Error())
return
}

Expand Down
4 changes: 2 additions & 2 deletions v4/core/base_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ func TestIAMFailure(t *testing.T) {
var foo *Foo
_, err = service.Request(req, &foo)
assert.NotNil(t, err)
assert.Equal(t, "Sorry you are forbidden", err.Error())
assert.Contains(t, err.Error(), "Sorry you are forbidden")
}

func TestIAMWithIdSecret(t *testing.T) {
Expand Down Expand Up @@ -1053,7 +1053,7 @@ func TestCP4DFail(t *testing.T) {

var foo *Foo
_, err = service.Request(req, &foo)
assert.Equal(t, "Sorry you are forbidden", err.Error())
assert.Contains(t, err.Error(), "Sorry you are forbidden")
}

// Test for the deprecated SetURL method.
Expand Down
3 changes: 3 additions & 0 deletions v4/core/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,7 @@ const (
"self-signed certificate, disable verification of the server's SSL certificate " +
"by invoking the DisableSSLVerification() function on your service instance " +
"and/or use the DisableSSLVerification option of the authenticator."
ERRORMSG_AUTHENTICATE_ERROR = "An error occurred while performing the 'authenticate' step: %s"
ERRORMSG_READ_RESPONSE_BODY = "An error occurred while reading the response body: %s"
ERRORMSG_UNMARSHAL_RESPONSE_BODY = "An error occurred while unmarshalling the response body: %s"
)

0 comments on commit c5dd77f

Please sign in to comment.