remove preset auth errors. they can be handled via StatusError without special cases

This commit is contained in:
Alex Grintsvayg 2018-08-22 12:05:30 -04:00
parent 69cce8e053
commit f06abb1f02
No known key found for this signature in database
GPG key ID: AEB3F089F86A22B5

View file

@ -27,20 +27,13 @@ var LogInfo = func(*http.Request, *Response) {}
// TraceEnabled Attaches a trace field to the JSON response when enabled.
var TraceEnabled = false
var ErrAuthenticationRequired = errors.Base("authentication required")
var ErrNotAuthenticated = errors.Base("could not authenticate user")
var ErrForbidden = errors.Base("you are not authorized to perform this action")
// StatusError represents an error with an associated HTTP status code.
type StatusError struct {
Status int
Err error
}
// Allows StatusError to satisfy the error interface.
func (se StatusError) Error() string {
return se.Err.Error()
}
func (se StatusError) Error() string { return se.Err.Error() }
// Response is returned by API handlers
type Response struct {
@ -87,10 +80,6 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if rsp.Error != nil {
if statusError, ok := rsp.Error.(StatusError); ok {
rsp.Status = statusError.Status
} else if errors.Is(rsp.Error, ErrAuthenticationRequired) {
rsp.Status = http.StatusUnauthorized
} else if errors.Is(rsp.Error, ErrNotAuthenticated) || errors.Is(rsp.Error, ErrForbidden) {
rsp.Status = http.StatusForbidden
} else {
rsp.Status = http.StatusInternalServerError
}