From c211b87183fb94eec61b37f8c486202ef355ce52 Mon Sep 17 00:00:00 2001 From: Becca Petrin Date: Tue, 4 Feb 2020 15:30:15 -0800 Subject: [PATCH] improve err and comments --- serviceregistration/kubernetes/client/client.go | 14 ++++++++++---- .../kubernetes/client/client_test.go | 8 ++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/serviceregistration/kubernetes/client/client.go b/serviceregistration/kubernetes/client/client.go index 1eea1bc03444..a66da2f971a2 100644 --- a/serviceregistration/kubernetes/client/client.go +++ b/serviceregistration/kubernetes/client/client.go @@ -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") ) @@ -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)) @@ -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) diff --git a/serviceregistration/kubernetes/client/client_test.go b/serviceregistration/kubernetes/client/client_test.go index c36b5367bbb7..877cac72504d 100644 --- a/serviceregistration/kubernetes/client/client_test.go +++ b/serviceregistration/kubernetes/client/client_test.go @@ -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) } } @@ -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) } }