authenticate: add additional error details for hmac errors (#3878)

This commit is contained in:
Caleb Doxsey 2023-01-11 07:53:11 -07:00 committed by GitHub
parent 92b50683ff
commit bfcd15435f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 7 deletions

View file

@ -16,7 +16,8 @@ type HTTPError struct {
// HTTP status codes as registered with IANA.
Status int
// Err is the wrapped error.
Err error
Err error
Description string
// DebugURL is the URL to the debug endpoint.
DebugURL *url.URL
// The request ID.
@ -26,7 +27,7 @@ type HTTPError struct {
}
// NewError returns an error that contains a HTTP status and error.
func NewError(status int, err error) error {
func NewError(status int, err error) *HTTPError {
return &HTTPError{Status: status, Err: err}
}
@ -54,6 +55,7 @@ func (e *HTTPError) ErrorResponse(ctx context.Context, w http.ResponseWriter, r
response := struct {
Status int
StatusText string `json:"-"`
Description string `json:"description,omitempty"`
RequestID string `json:",omitempty"`
CanDebug bool `json:"-"`
DebugURL *url.URL `json:",omitempty"`
@ -61,6 +63,7 @@ func (e *HTTPError) ErrorResponse(ctx context.Context, w http.ResponseWriter, r
}{
Status: e.Status,
StatusText: StatusText(e.Status),
Description: e.Description,
RequestID: reqID,
CanDebug: e.Status/100 == 4 && (e.DebugURL != nil || reqID != ""),
DebugURL: e.DebugURL,
@ -85,6 +88,7 @@ func (e *HTTPError) ErrorResponse(ctx context.Context, w http.ResponseWriter, r
m := map[string]any{
"canDebug": response.CanDebug,
"description": response.Description,
"requestId": response.RequestID,
"status": response.Status,
"statusText": response.StatusText,
@ -101,3 +105,9 @@ func (e *HTTPError) ErrorResponse(ctx context.Context, w http.ResponseWriter, r
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// WithDescription sets the description in the HTTP error.
func (e *HTTPError) WithDescription(description string) *HTTPError {
e.Description = description
return e
}