From 9656c7a0fea64b482bcf22c44370aeb1898cc1e2 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Thu, 11 Jan 2024 16:51:24 -0600 Subject: [PATCH 1/4] coder error first --- enterprise/coderd/proxyhealth/proxyhealth.go | 29 +++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/enterprise/coderd/proxyhealth/proxyhealth.go b/enterprise/coderd/proxyhealth/proxyhealth.go index f4014e398135b..08508645f5450 100644 --- a/enterprise/coderd/proxyhealth/proxyhealth.go +++ b/enterprise/coderd/proxyhealth/proxyhealth.go @@ -3,6 +3,7 @@ package proxyhealth import ( "context" "encoding/json" + "errors" "fmt" "net/http" "net/url" @@ -275,8 +276,34 @@ func (p *ProxyHealth) runOnce(ctx context.Context, now time.Time) (map[uuid.UUID case err == nil && resp.StatusCode == http.StatusOK: err := json.NewDecoder(resp.Body).Decode(&status.Report) if err != nil { + isCoderErr := fmt.Errorf("proxy url %q is not a coder proxy instance, verify the url is correct", reqURL) + if resp.Header.Get(codersdk.BuildVersionHeader) != "" { + isCoderErr = fmt.Errorf("proxy url %q is a coder instance, but unable to decode the response payload. Could this be a primary coderd and not a proxy?", reqURL) + } + + // If the response is not json, then the user likely input a bad url that returns status code 200. + // This is very common, since most webpages do return a 200. So let's improve the error message. + if notJsonErr := codersdk.ExpectJSONMime(resp); notJsonErr != nil { + + err = errors.Join( + isCoderErr, + fmt.Errorf("attempted to query health at %q but got back the incorrect content type: %w", reqURL, notJsonErr), + ) + + status.Report.Errors = []string{ + err.Error(), + } + status.Status = Unhealthy + break + } + // If we cannot read the report, mark the proxy as unhealthy. - status.Report.Errors = []string{fmt.Sprintf("failed to decode health report: %s", err.Error())} + status.Report.Errors = []string{ + errors.Join( + isCoderErr, + fmt.Errorf("received a status code 200, but failed to decode health report body: %w", err.Error()), + ).Error(), + } status.Status = Unhealthy break } From 16b50de674dc34a2a21b02dc80204d4f49ac0ac9 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 12 Jan 2024 08:56:45 -0600 Subject: [PATCH 2/4] lint --- enterprise/coderd/proxyhealth/proxyhealth.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/enterprise/coderd/proxyhealth/proxyhealth.go b/enterprise/coderd/proxyhealth/proxyhealth.go index 08508645f5450..85556dacfb73b 100644 --- a/enterprise/coderd/proxyhealth/proxyhealth.go +++ b/enterprise/coderd/proxyhealth/proxyhealth.go @@ -301,7 +301,7 @@ func (p *ProxyHealth) runOnce(ctx context.Context, now time.Time) (map[uuid.UUID status.Report.Errors = []string{ errors.Join( isCoderErr, - fmt.Errorf("received a status code 200, but failed to decode health report body: %w", err.Error()), + fmt.Errorf("received a status code 200, but failed to decode health report body: %w", err), ).Error(), } status.Status = Unhealthy From e1f8c411fc353d258e954de3d7cd9d311c97655e Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 12 Jan 2024 11:52:41 -0600 Subject: [PATCH 3/4] linting --- enterprise/coderd/proxyhealth/proxyhealth.go | 1 - 1 file changed, 1 deletion(-) diff --git a/enterprise/coderd/proxyhealth/proxyhealth.go b/enterprise/coderd/proxyhealth/proxyhealth.go index 85556dacfb73b..83fec25c1f440 100644 --- a/enterprise/coderd/proxyhealth/proxyhealth.go +++ b/enterprise/coderd/proxyhealth/proxyhealth.go @@ -284,7 +284,6 @@ func (p *ProxyHealth) runOnce(ctx context.Context, now time.Time) (map[uuid.UUID // If the response is not json, then the user likely input a bad url that returns status code 200. // This is very common, since most webpages do return a 200. So let's improve the error message. if notJsonErr := codersdk.ExpectJSONMime(resp); notJsonErr != nil { - err = errors.Join( isCoderErr, fmt.Errorf("attempted to query health at %q but got back the incorrect content type: %w", reqURL, notJsonErr), From 31450af97982ae8fe99b92d744c6160fdb0be0e9 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 12 Jan 2024 14:05:04 -0600 Subject: [PATCH 4/4] json -> JSON --- enterprise/coderd/proxyhealth/proxyhealth.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/enterprise/coderd/proxyhealth/proxyhealth.go b/enterprise/coderd/proxyhealth/proxyhealth.go index 83fec25c1f440..56a2fe4e1fa22 100644 --- a/enterprise/coderd/proxyhealth/proxyhealth.go +++ b/enterprise/coderd/proxyhealth/proxyhealth.go @@ -283,10 +283,10 @@ func (p *ProxyHealth) runOnce(ctx context.Context, now time.Time) (map[uuid.UUID // If the response is not json, then the user likely input a bad url that returns status code 200. // This is very common, since most webpages do return a 200. So let's improve the error message. - if notJsonErr := codersdk.ExpectJSONMime(resp); notJsonErr != nil { + if notJSONErr := codersdk.ExpectJSONMime(resp); notJSONErr != nil { err = errors.Join( isCoderErr, - fmt.Errorf("attempted to query health at %q but got back the incorrect content type: %w", reqURL, notJsonErr), + fmt.Errorf("attempted to query health at %q but got back the incorrect content type: %w", reqURL, notJSONErr), ) status.Report.Errors = []string{