Browse code

Use apiError in server version middleware.

Signed-off-by: Daniel Nephin <dnephin@docker.com>

Daniel Nephin authored on 2016/09/16 02:30:07
Showing 2 changed files
... ...
@@ -75,13 +75,18 @@ func GetHTTPErrorStatusCode(err error) int {
75 75
 	return statusCode
76 76
 }
77 77
 
78
+func apiVersionSupportsJSONErrors(version string) bool {
79
+	const firstAPIVersionWithJSONErrors = "1.23"
80
+	return version == "" || versions.GreaterThan(version, firstAPIVersionWithJSONErrors)
81
+}
82
+
78 83
 // MakeErrorHandler makes an HTTP handler that decodes a Docker error and
79 84
 // returns it in the response.
80 85
 func MakeErrorHandler(err error) http.HandlerFunc {
81 86
 	return func(w http.ResponseWriter, r *http.Request) {
82 87
 		statusCode := GetHTTPErrorStatusCode(err)
83 88
 		vars := mux.Vars(r)
84
-		if vars["version"] == "" || versions.GreaterThan(vars["version"], "1.23") {
89
+		if apiVersionSupportsJSONErrors(vars["version"]) {
85 90
 			response := &types.ErrorResponse{
86 91
 				Message: err.Error(),
87 92
 			}
... ...
@@ -5,18 +5,11 @@ import (
5 5
 	"net/http"
6 6
 	"runtime"
7 7
 
8
+	"github.com/docker/docker/api/errors"
8 9
 	"github.com/docker/docker/api/types/versions"
9 10
 	"golang.org/x/net/context"
10 11
 )
11 12
 
12
-type badRequestError struct {
13
-	error
14
-}
15
-
16
-func (badRequestError) HTTPErrorStatusCode() int {
17
-	return http.StatusBadRequest
18
-}
19
-
20 13
 // VersionMiddleware is a middleware that
21 14
 // validates the client and server versions.
22 15
 type VersionMiddleware struct {
... ...
@@ -44,10 +37,10 @@ func (v VersionMiddleware) WrapHandler(handler func(ctx context.Context, w http.
44 44
 		}
45 45
 
46 46
 		if versions.GreaterThan(apiVersion, v.defaultVersion) {
47
-			return badRequestError{fmt.Errorf("client is newer than server (client API version: %s, server API version: %s)", apiVersion, v.defaultVersion)}
47
+			return errors.NewBadRequestError(fmt.Errorf("client is newer than server (client API version: %s, server API version: %s)", apiVersion, v.defaultVersion))
48 48
 		}
49 49
 		if versions.LessThan(apiVersion, v.minVersion) {
50
-			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)}
50
+			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))
51 51
 		}
52 52
 
53 53
 		header := fmt.Sprintf("Docker/%s (%s)", v.serverVersion, runtime.GOOS)