Skip to content

Commit b8a1435

Browse files
authored
fix: use *string instead of error in healthcheck response (#8234)
1 parent e2e07b0 commit b8a1435

File tree

13 files changed

+195
-114
lines changed

13 files changed

+195
-114
lines changed

coderd/apidoc/docs.go

Lines changed: 32 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 32 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/healthcheck/accessurl.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ import (
88
"time"
99

1010
"golang.org/x/xerrors"
11+
12+
"github.com/coder/coder/coderd/util/ptr"
1113
)
1214

1315
type AccessURLReport struct {
14-
Healthy bool `json:"healthy"`
15-
Reachable bool `json:"reachable"`
16-
StatusCode int `json:"status_code"`
17-
HealthzResponse string `json:"healthz_response"`
18-
Error error `json:"error"`
16+
AccessURL string `json:"access_url"`
17+
Healthy bool `json:"healthy"`
18+
Reachable bool `json:"reachable"`
19+
StatusCode int `json:"status_code"`
20+
HealthzResponse string `json:"healthz_response"`
21+
Error *string `json:"error"`
1922
}
2023

2124
type AccessURLReportOptions struct {
@@ -28,36 +31,37 @@ func (r *AccessURLReport) Run(ctx context.Context, opts *AccessURLReportOptions)
2831
defer cancel()
2932

3033
if opts.AccessURL == nil {
31-
r.Error = xerrors.New("access URL is nil")
34+
r.Error = ptr.Ref("access URL is nil")
3235
return
3336
}
37+
r.AccessURL = opts.AccessURL.String()
3438

3539
if opts.Client == nil {
3640
opts.Client = http.DefaultClient
3741
}
3842

3943
accessURL, err := opts.AccessURL.Parse("/healthz")
4044
if err != nil {
41-
r.Error = xerrors.Errorf("parse healthz endpoint: %w", err)
45+
r.Error = convertError(xerrors.Errorf("parse healthz endpoint: %w", err))
4246
return
4347
}
4448

4549
req, err := http.NewRequestWithContext(ctx, "GET", accessURL.String(), nil)
4650
if err != nil {
47-
r.Error = xerrors.Errorf("create healthz request: %w", err)
51+
r.Error = convertError(xerrors.Errorf("create healthz request: %w", err))
4852
return
4953
}
5054

5155
res, err := opts.Client.Do(req)
5256
if err != nil {
53-
r.Error = xerrors.Errorf("get healthz endpoint: %w", err)
57+
r.Error = convertError(xerrors.Errorf("get healthz endpoint: %w", err))
5458
return
5559
}
5660
defer res.Body.Close()
5761

5862
body, err := io.ReadAll(res.Body)
5963
if err != nil {
60-
r.Error = xerrors.Errorf("read healthz response: %w", err)
64+
r.Error = convertError(xerrors.Errorf("read healthz response: %w", err))
6165
return
6266
}
6367

coderd/healthcheck/accessurl_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestAccessURL(t *testing.T) {
3636
assert.True(t, report.Reachable)
3737
assert.Equal(t, http.StatusOK, report.StatusCode)
3838
assert.Equal(t, "OK", report.HealthzResponse)
39-
assert.NoError(t, report.Error)
39+
assert.Nil(t, report.Error)
4040
})
4141

4242
t.Run("404", func(t *testing.T) {
@@ -66,7 +66,7 @@ func TestAccessURL(t *testing.T) {
6666
assert.True(t, report.Reachable)
6767
assert.Equal(t, http.StatusNotFound, report.StatusCode)
6868
assert.Equal(t, string(resp), report.HealthzResponse)
69-
assert.NoError(t, report.Error)
69+
assert.Nil(t, report.Error)
7070
})
7171

7272
t.Run("ClientErr", func(t *testing.T) {
@@ -102,7 +102,8 @@ func TestAccessURL(t *testing.T) {
102102
assert.False(t, report.Reachable)
103103
assert.Equal(t, 0, report.StatusCode)
104104
assert.Equal(t, "", report.HealthzResponse)
105-
assert.ErrorIs(t, report.Error, expErr)
105+
require.NotNil(t, report.Error)
106+
assert.Contains(t, *report.Error, expErr.Error())
106107
})
107108
}
108109

coderd/healthcheck/database.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type DatabaseReport struct {
1414
Healthy bool `json:"healthy"`
1515
Reachable bool `json:"reachable"`
1616
Latency time.Duration `json:"latency"`
17-
Error error `json:"error"`
17+
Error *string `json:"error"`
1818
}
1919

2020
type DatabaseReportOptions struct {
@@ -31,7 +31,7 @@ func (r *DatabaseReport) Run(ctx context.Context, opts *DatabaseReportOptions) {
3131
for i := 0; i < pingCount; i++ {
3232
pong, err := opts.DB.Ping(ctx)
3333
if err != nil {
34-
r.Error = xerrors.Errorf("ping: %w", err)
34+
r.Error = convertError(xerrors.Errorf("ping: %w", err))
3535
return
3636
}
3737
pings = append(pings, pong)

coderd/healthcheck/database_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/golang/mock/gomock"
99
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
1011
"golang.org/x/xerrors"
1112

1213
"github.com/coder/coder/coderd/database/dbmock"
@@ -35,7 +36,7 @@ func TestDatabase(t *testing.T) {
3536
assert.True(t, report.Healthy)
3637
assert.True(t, report.Reachable)
3738
assert.Equal(t, ping, report.Latency)
38-
assert.NoError(t, report.Error)
39+
assert.Nil(t, report.Error)
3940
})
4041

4142
t.Run("Error", func(t *testing.T) {
@@ -56,7 +57,8 @@ func TestDatabase(t *testing.T) {
5657
assert.False(t, report.Healthy)
5758
assert.False(t, report.Reachable)
5859
assert.Zero(t, report.Latency)
59-
assert.ErrorIs(t, report.Error, err)
60+
require.NotNil(t, report.Error)
61+
assert.Contains(t, *report.Error, err.Error())
6062
})
6163

6264
t.Run("Median", func(t *testing.T) {
@@ -80,6 +82,6 @@ func TestDatabase(t *testing.T) {
8082
assert.True(t, report.Healthy)
8183
assert.True(t, report.Reachable)
8284
assert.Equal(t, time.Millisecond, report.Latency)
83-
assert.NoError(t, report.Error)
85+
assert.Nil(t, report.Error)
8486
})
8587
}

0 commit comments

Comments
 (0)