Skip to content

Commit

Permalink
Use apiError in server version middleware.
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Nephin <[email protected]>
  • Loading branch information
dnephin committed Sep 16, 2016
1 parent c452e1b commit bcb0405
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
7 changes: 6 additions & 1 deletion api/server/httputils/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,18 @@ func GetHTTPErrorStatusCode(err error) int {
return statusCode
}

func apiVersionSupportsJSONErrors(version string) bool {
const firstAPIVersionWithJSONErrors = "1.23"
return version == "" || versions.GreaterThan(version, firstAPIVersionWithJSONErrors)
}

// MakeErrorHandler makes an HTTP handler that decodes a Docker error and
// returns it in the response.
func MakeErrorHandler(err error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
statusCode := GetHTTPErrorStatusCode(err)
vars := mux.Vars(r)
if vars["version"] == "" || versions.GreaterThan(vars["version"], "1.23") {
if apiVersionSupportsJSONErrors(vars["version"]) {
response := &types.ErrorResponse{
Message: err.Error(),
}
Expand Down
13 changes: 3 additions & 10 deletions api/server/middleware/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@ import (
"net/http"
"runtime"

"github.com/docker/docker/api/errors"
"github.com/docker/docker/api/types/versions"
"golang.org/x/net/context"
)

type badRequestError struct {
error
}

func (badRequestError) HTTPErrorStatusCode() int {
return http.StatusBadRequest
}

// VersionMiddleware is a middleware that
// validates the client and server versions.
type VersionMiddleware struct {
Expand Down Expand Up @@ -44,10 +37,10 @@ func (v VersionMiddleware) WrapHandler(handler func(ctx context.Context, w http.
}

if versions.GreaterThan(apiVersion, v.defaultVersion) {
return badRequestError{fmt.Errorf("client is newer than server (client API version: %s, server API version: %s)", apiVersion, v.defaultVersion)}
return errors.NewBadRequestError(fmt.Errorf("client is newer than server (client API version: %s, server API version: %s)", apiVersion, v.defaultVersion))
}
if versions.LessThan(apiVersion, v.minVersion) {
return badRequestError{fmt.Errorf("client version %s is too old. Minimum supported API version is %s, please upgrade your client to a newer version", apiVersion, v.minVersion)}
return errors.NewBadRequestError(fmt.Errorf("client version %s is too old. Minimum supported API version is %s, please upgrade your client to a newer version", apiVersion, v.minVersion))
}

header := fmt.Sprintf("Docker/%s (%s)", v.serverVersion, runtime.GOOS)
Expand Down

0 comments on commit bcb0405

Please sign in to comment.