-
Notifications
You must be signed in to change notification settings - Fork 25
Add more elaborate health check endpoint that optionally hits the database #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,11 @@ import ( | |
| // | ||
| // APIErrorInterface should be used with errors.As instead of this struct. | ||
| type APIError struct { | ||
| // InternalError is an additional error that might be associated with the | ||
| // API error. It's not returned in the API error response, but is logged in | ||
| // API endpoint execution to provide extra information for operators. | ||
| InternalError error `json:"-"` | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a possible internal error that an API error can be tagged with and which will be added to the logs by
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love it! |
||
|
|
||
| // Message is a descriptive, human-friendly message indicating what went | ||
| // wrong. Try to make error messages as actionable as possible to help the | ||
| // caller easily fix what went wrong. | ||
|
|
@@ -26,7 +31,9 @@ type APIError struct { | |
| StatusCode int `json:"-"` | ||
| } | ||
|
|
||
| func (e *APIError) Error() string { return e.Message } | ||
| func (e *APIError) Error() string { return e.Message } | ||
| func (e *APIError) GetInternalError() error { return e.InternalError } | ||
| func (e *APIError) SetInternalError(internalErr error) { e.InternalError = internalErr } | ||
|
|
||
| // Write writes the API error to an HTTP response, writing to the given logger | ||
| // in case of a problem. | ||
|
|
@@ -48,9 +55,18 @@ func (e *APIError) Write(ctx context.Context, logger *slog.Logger, w http.Respon | |
| // won't be usable as an errors.As target. | ||
| type Interface interface { | ||
| Error() string | ||
| GetInternalError() error | ||
| SetInternalError(internalErr error) | ||
| Write(ctx context.Context, logger *slog.Logger, w http.ResponseWriter) | ||
| } | ||
|
|
||
| // WithInternalError is a convenience function for assigning an internal error | ||
| // to the given API error and returning it. | ||
| func WithInternalError[TAPIError Interface](apiErr TAPIError, internalErr error) TAPIError { | ||
| apiErr.SetInternalError(internalErr) | ||
| return apiErr | ||
| } | ||
|
|
||
| // | ||
| // BadRequest | ||
| // | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A lot of endpoints return
{status: "ok"}, so I made a common struct that they can share so we don't have to write a new response struct for each.statusResponseOKbelow is also a shortcut for making an OK response.