Skip to content

Commit

Permalink
fix: properly expose errors in health checker
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Jul 16, 2022
1 parent 4e819dc commit 21b593b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions healthx/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
// Package healthx providers helpers for returning health status information via HTTP.
package healthx

import "strings"

// swagger:model healthStatus
type swaggerHealthStatus struct {
// Status always contains "ok".
Expand All @@ -33,6 +35,14 @@ type swaggerNotReadyStatus struct {
Errors map[string]string `json:"errors"`
}

func (s swaggerNotReadyStatus) Error() string {
var errs []string
for _, err := range s.Errors {
errs = append(errs, err)
}
return strings.Join(errs, "; ")
}

// swagger:model version
type swaggerVersion struct {
// Version is the service's version.
Expand Down
2 changes: 1 addition & 1 deletion healthx/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (h *Handler) Ready(shareErrors bool) http.Handler {
}

if len(notReady.Errors) > 0 {
h.H.WriteCode(rw, r, http.StatusServiceUnavailable, notReady)
h.H.WriteErrorCode(rw, r, http.StatusServiceUnavailable, &notReady)
return
}

Expand Down
2 changes: 1 addition & 1 deletion healthx/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestHealth(t *testing.T) {
require.EqualValues(t, http.StatusServiceUnavailable, response.StatusCode)
out, err := ioutil.ReadAll(response.Body)
require.NoError(t, err)
assert.Equal(t, `{"errors":{"test":"not alive"}}`, strings.TrimSpace(string(out)))
assert.Equal(t, "{\"error\":{\"code\":500,\"status\":\"Internal Server Error\",\"message\":\"not alive\"}}", strings.TrimSpace(string(out)))
return response
}

Expand Down

0 comments on commit 21b593b

Please sign in to comment.