Instrustment handlers for logging/error handling

This refactors a bit of the http handler code.
Moves error handling for handler functions to a generic handler.
This also has a side-effect of being able to propagate errors from the
provider to send the correct status code, provided the error type
matches a pre-defined interface.
This commit is contained in:
Brian Goff
2018-09-17 16:35:17 -07:00
parent 8eb6ab4bcd
commit 74f76c75d5
9 changed files with 890 additions and 33 deletions

37
vendor/github.com/cpuguy83/strongerrors/status/http.go generated vendored Normal file
View File

@@ -0,0 +1,37 @@
package status
import (
"net/http"
"github.com/cpuguy83/strongerrors"
)
// HTTPCode takes an error and returns the HTTP status code for the given error
// If a match is found then the second return argument will be true, otherwise it will be false.
// nolint: gocyclo
func HTTPCode(err error) (int, bool) {
switch {
case strongerrors.IsNotFound(err):
return http.StatusNotFound, true
case strongerrors.IsInvalidArgument(err):
return http.StatusBadRequest, true
case strongerrors.IsConflict(err):
return http.StatusConflict, true
case strongerrors.IsUnauthenticated(err), strongerrors.IsForbidden(err):
return http.StatusForbidden, true
case strongerrors.IsUnauthorized(err):
return http.StatusUnauthorized, true
case strongerrors.IsUnavailable(err):
return http.StatusServiceUnavailable, true
case strongerrors.IsForbidden(err):
return http.StatusForbidden, true
case strongerrors.IsAlreadyExists(err), strongerrors.IsNotModified(err):
return http.StatusNotModified, true
case strongerrors.IsNotImplemented(err):
return http.StatusNotImplemented, true
case strongerrors.IsSystem(err) || strongerrors.IsUnknown(err) || strongerrors.IsDataLoss(err) || strongerrors.IsExhausted(err):
return http.StatusInternalServerError, true
default:
return http.StatusInternalServerError, false
}
}