Files
virtual-kubelet/vendor/github.com/cpuguy83/strongerrors/status/http.go
Brian Goff 74f76c75d5 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.
2018-09-17 16:54:24 -07:00

38 lines
1.3 KiB
Go

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
}
}