Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
fix: identify http request errors as network errors (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
myishay authored Jul 4, 2022
1 parent d7d1da6 commit f758f0a
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion bl/validation/k8sValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (val *K8sValidator) validateResource(filepath string) (bool, []error, *vali
isValid = false
errString := res.Err.Error()

if utils.IsNetworkError(errString) {
if utils.IsNetworkError(res.Err) {
validationErrors = append(validationErrors, &InvalidK8sSchemaError{errString})
} else {
errorMessages := strings.Split(errString, "-")
Expand Down
2 changes: 1 addition & 1 deletion pkg/cliClient/cliClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type HTTPClient interface {
Request(method string, resourceURI string, body interface{}, headers map[string]string) (httpClient.Response, error)
}
type NetworkValidator interface {
IdentifyNetworkError(errStr string) error
IdentifyNetworkError(err error) error
IsLocalMode() bool
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/cliClient/evaluation.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (c *CliClient) RequestEvaluationPrerunData(tokenId string, isCi bool) (*Eva
res, err := c.httpClient.Request(http.MethodGet, "/cli/evaluation/tokens/"+tokenId+"/prerun?"+isCiQueryParam, nil, c.flagsHeaders)

if err != nil {
networkErr := c.networkValidator.IdentifyNetworkError(err.Error())
networkErr := c.networkValidator.IdentifyNetworkError(err)
if networkErr != nil {
return &EvaluationPrerunDataResponse{}, networkErr
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func (c *CliClient) SendEvaluationResult(request *EvaluationResultRequest) (*Sen

httpRes, err := c.httpClient.Request(http.MethodPost, "/cli/evaluation/result", request, c.flagsHeaders)
if err != nil {
networkErr := c.networkValidator.IdentifyNetworkError(err.Error())
networkErr := c.networkValidator.IdentifyNetworkError(err)
if networkErr != nil {
return &SendEvaluationResultsResponse{}, networkErr
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cliClient/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (c *CliClient) CreateToken() (*CreateTokenResponse, error) {
res, err := c.httpClient.Request(http.MethodPost, "/cli/tokens/", nil, headers)

if err != nil {
networkErr := c.networkValidator.IdentifyNetworkError(err.Error())
networkErr := c.networkValidator.IdentifyNetworkError(err)
if networkErr != nil {
return nil, networkErr
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/networkValidator/networkValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func (nv *NetworkValidator) SetOfflineMode(offlineMode string) {
nv.offlineMode = offlineMode
}

func (nv *NetworkValidator) IdentifyNetworkError(errStr string) error {
if utils.IsNetworkError(errStr) {
func (nv *NetworkValidator) IdentifyNetworkError(err error) error {
if utils.IsNetworkError(err) {
if nv.offlineMode == "fail" {
return errors.New("Failed since internet connection refused, you can use the following command to set your config to run offline:\ndatree config set offline local")
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/networkValidator/validator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package networkValidator

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -58,20 +59,20 @@ func TestNetworkValidatorOtherError(t *testing.T) {

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

func test_identifyNoInternet_noSuchHost_network_error(validator *NetworkValidator, offlineMode string) error {
validator.SetOfflineMode(offlineMode)
return validator.IdentifyNetworkError("no such host")
return validator.IdentifyNetworkError(errors.New("no such host"))
}

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

func test_identifyNetworkError_other_error(validator *NetworkValidator, offlineMode string) error {
validator.SetOfflineMode(offlineMode)
return validator.IdentifyNetworkError("mysql server is away")
return validator.IdentifyNetworkError(errors.New("mysql server is away"))
}
10 changes: 8 additions & 2 deletions pkg/utils/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"fmt"
"net/url"
"strings"
)

Expand All @@ -16,9 +17,9 @@ func ParseErrorToString(err interface{}) string {
}
}

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

func stringInSliceContains(a string, list []string) bool {
Expand All @@ -29,3 +30,8 @@ func stringInSliceContains(a string, list []string) bool {
}
return false
}

func isUrlErrorType(err error) bool {
_, ok := err.(*url.Error)
return ok
}

0 comments on commit f758f0a

Please sign in to comment.