Skip to content

Commit

Permalink
improve err and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrannosaurus-becks committed Feb 4, 2020
1 parent e75fe3b commit c211b87
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
14 changes: 10 additions & 4 deletions serviceregistration/kubernetes/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const maxRetries = 10
var (
ErrNamespaceUnset = errors.New(`"namespace" is unset`)
ErrPodNameUnset = errors.New(`"podName" is unset`)
ErrNotFound = errors.New("not found")
ErrNotInCluster = errors.New("unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined")
)

Expand Down Expand Up @@ -194,7 +193,7 @@ func (c *Client) attemptRequest(client *http.Client, req *http.Request, ptrToRet
// Continue to try again, but return the error too in case the caller would rather read it out.
return true, fmt.Errorf("bad status code: %s", sanitizedDebuggingInfo(req, reqBody, resp))
case 404:
return false, ErrNotFound
return false, &ErrNotFound{debuggingInfo: sanitizedDebuggingInfo(req, reqBody, resp)}
case 500, 502, 503, 504:
// Could be transient.
return true, fmt.Errorf("unexpected status code: %s", sanitizedDebuggingInfo(req, reqBody, resp))
Expand Down Expand Up @@ -273,9 +272,16 @@ type Patch struct {
Value interface{}
}

type ErrNotFound struct {
debuggingInfo string
}

func (e *ErrNotFound) Error() string {
return e.debuggingInfo
}

// sanitizedDebuggingInfo converts an http response to a string without
// including its headers to avoid leaking authorization
// headers.
// including its headers to avoid leaking authorization headers.
func sanitizedDebuggingInfo(req *http.Request, reqBody []byte, resp *http.Response) string {
respBody, _ := ioutil.ReadAll(resp.Body)
return fmt.Sprintf("req method: %s, req url: %s, req body: %s, resp statuscode: %d, resp respBody: %s", req.Method, req.URL, reqBody, resp.StatusCode, respBody)
Expand Down
8 changes: 4 additions & 4 deletions serviceregistration/kubernetes/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func (e *env) TestGetPodNotFound(t *testing.T) {
if err == nil {
t.Fatal("expected error because pod is unfound")
}
if err != ErrNotFound {
t.Fatalf("expected %q but received %q", ErrNotFound, err)
if _, ok := err.(*ErrNotFound); !ok {
t.Fatalf("expected *ErrNotFound but received %T", err)
}
}

Expand Down Expand Up @@ -86,7 +86,7 @@ func (e *env) TestUpdatePodTagsNotFound(t *testing.T) {
if err == nil {
t.Fatal("expected error because pod is unfound")
}
if err != ErrNotFound {
t.Fatalf("expected %q but received %q", ErrNotFound, err)
if _, ok := err.(*ErrNotFound); !ok {
t.Fatalf("expected *ErrNotFound but received %T", err)
}
}

0 comments on commit c211b87

Please sign in to comment.