diff options
Diffstat (limited to 'api/server/httputils/errors.go')
| -rw-r--r-- | api/server/httputils/errors.go | 78 |
1 files changed, 36 insertions, 42 deletions
diff --git a/api/server/httputils/errors.go b/api/server/httputils/errors.go index b677f95d6a..10628f6b33 100644 --- a/api/server/httputils/errors.go +++ b/api/server/httputils/errors.go @@ -1,9 +1,10 @@ package httputils import ( + "fmt" "net/http" - "strings" + "github.com/docker/docker/api/errdefs" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/versions" "github.com/gorilla/mux" @@ -20,13 +21,8 @@ type httpStatusError interface { HTTPErrorStatusCode() int } -// inputValidationError is an interface -// that errors generated by invalid -// inputs can implement to tell the -// api layer to set a 400 status code -// in the response. -type inputValidationError interface { - IsValidationError() bool +type causer interface { + Cause() error } // GetHTTPErrorStatusCode retrieves status code from error message. @@ -37,49 +33,44 @@ func GetHTTPErrorStatusCode(err error) int { } var statusCode int - errMsg := err.Error() - switch e := err.(type) { - case httpStatusError: - statusCode = e.HTTPErrorStatusCode() - case inputValidationError: + // Stop right there + // Are you sure you should be adding a new error class here? Do one of the existing ones work? + + // Note that the below functions are already checking the error causal chain for matches. + switch { + case errdefs.IsNotFound(err): + statusCode = http.StatusNotFound + case errdefs.IsInvalidParameter(err): statusCode = http.StatusBadRequest + case errdefs.IsConflict(err): + statusCode = http.StatusConflict + case errdefs.IsUnauthorized(err): + statusCode = http.StatusUnauthorized + case errdefs.IsUnavailable(err): + statusCode = http.StatusServiceUnavailable + case errdefs.IsForbidden(err): + statusCode = http.StatusForbidden + case errdefs.IsNotModified(err): + statusCode = http.StatusNotModified + case errdefs.IsNotImplemented(err): + statusCode = http.StatusNotImplemented + case errdefs.IsSystem(err) || errdefs.IsUnknown(err): + statusCode = http.StatusInternalServerError default: statusCode = statusCodeFromGRPCError(err) if statusCode != http.StatusInternalServerError { return statusCode } - // FIXME: this is brittle and should not be necessary, but we still need to identify if - // there are errors falling back into this logic. - // If we need to differentiate between different possible error types, - // we should create appropriate error types that implement the httpStatusError interface. - errStr := strings.ToLower(errMsg) - - for _, status := range []struct { - keyword string - code int - }{ - {"not found", http.StatusNotFound}, - {"cannot find", http.StatusNotFound}, - {"no such", http.StatusNotFound}, - {"bad parameter", http.StatusBadRequest}, - {"no command", http.StatusBadRequest}, - {"conflict", http.StatusConflict}, - {"impossible", http.StatusNotAcceptable}, - {"wrong login/password", http.StatusUnauthorized}, - {"unauthorized", http.StatusUnauthorized}, - {"hasn't been activated", http.StatusForbidden}, - {"this node", http.StatusServiceUnavailable}, - {"needs to be unlocked", http.StatusServiceUnavailable}, - {"certificates have expired", http.StatusServiceUnavailable}, - {"repository does not exist", http.StatusNotFound}, - } { - if strings.Contains(errStr, status.keyword) { - statusCode = status.code - break - } + if e, ok := err.(causer); ok { + return GetHTTPErrorStatusCode(e.Cause()) } + + logrus.WithFields(logrus.Fields{ + "module": "api", + "error_type": fmt.Sprintf("%T", err), + }).Debugf("FIXME: Got an API for which error does not match any expected type!!!: %+v", err) } if statusCode == 0 { @@ -133,6 +124,9 @@ func statusCodeFromGRPCError(err error) int { case codes.Unavailable: // code 14 return http.StatusServiceUnavailable default: + if e, ok := err.(causer); ok { + return statusCodeFromGRPCError(e.Cause()) + } // codes.Canceled(1) // codes.Unknown(2) // codes.DeadlineExceeded(4) |
