Skip to content

Commit

Permalink
fix: wrong error printed on server error (datreeio#765)
Browse files Browse the repository at this point in the history
* fix: wrongfully print network error

* test: replace http error with connection refused

Co-authored-by: shalev avhar <shalev@datree.io>
  • Loading branch information
shalev007 and shalev avhar authored Aug 25, 2022
1 parent 72c6d6e commit b431df2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pkg/cliClient/cliClient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func test_requestEvaluationPrerunData_network_error(offlineMode string, expected
}{
status: http.StatusInternalServerError,
body: nil,
error: errors.New("http error"),
error: errors.New("connection refused"),
},
},
expected: struct {
Expand Down Expand Up @@ -678,7 +678,7 @@ func test_sendEvaluationResult_network_error(offlineMode string, expectedRespons
}{
status: http.StatusInternalServerError,
body: nil,
error: errors.New("http error"),
error: errors.New("connection refused"),
},
},
expected: struct {
Expand Down
11 changes: 10 additions & 1 deletion pkg/httpClient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,16 @@ func (c *Client) Request(method string, resourceURI string, body interface{}, he
}

if response.StatusCode > 399 {
return responseBody, fmt.Errorf("http error: %s", string(b))
var errorResponse struct {
Message string `json:"message"`
}

err := json.Unmarshal(b, &errorResponse)
if err != nil || errorResponse.Message == "" {
return responseBody, fmt.Errorf("http error: %s", string(b))
}

return responseBody, fmt.Errorf(errorResponse.Message)
}

return responseBody, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/networkValidator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestNetworkValidatorOtherError(t *testing.T) {

func test_identifyNetworkError_network_error(validator *NetworkValidator, offlineMode string) error {
validator.SetOfflineMode(offlineMode)
return validator.IdentifyNetworkError(errors.New("http error"))
return validator.IdentifyNetworkError(errors.New("connection refused"))
}

func test_identifyNoInternet_noSuchHost_network_error(validator *NetworkValidator, offlineMode string) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func ParseErrorToString(err interface{}) string {
}

func IsNetworkError(err error) bool {
networkErrors := []string{"network error", "connection refused", "no such host", "i/o timeout", "server misbehaving", "http error"}
networkErrors := []string{"network error", "connection refused", "no such host", "i/o timeout", "server misbehaving"}
return stringInSliceContains(err.Error(), networkErrors) || isUrlErrorType(err)
}

Expand Down

0 comments on commit b431df2

Please sign in to comment.