summaryrefslogtreecommitdiff
path: root/api/server/middleware
diff options
context:
space:
mode:
authorBrian Goff <cpuguy83@gmail.com>2017-07-19 10:20:13 -0400
committerBrian Goff <cpuguy83@gmail.com>2017-08-15 16:01:11 -0400
commitebcb7d6b406fe50ea9a237c73004d75884184c33 (patch)
treec87316ace5b2ddc5efabeb311d47e98fd4113ce9 /api/server/middleware
parentb6498340b2baa6596553b2b56b43990a365a7b6a (diff)
downloaddocker-ebcb7d6b406fe50ea9a237c73004d75884184c33.tar.gz
Remove string checking in API error handling
Use strongly typed errors to set HTTP status codes. Error interfaces are defined in the api/errors package and errors returned from controllers are checked against these interfaces. Errors can be wraeped in a pkg/errors.Causer, as long as somewhere in the line of causes one of the interfaces is implemented. The special error interfaces take precedence over Causer, meaning if both Causer and one of the new error interfaces are implemented, the Causer is not traversed. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
Diffstat (limited to 'api/server/middleware')
-rw-r--r--api/server/middleware/version.go13
1 files changed, 11 insertions, 2 deletions
diff --git a/api/server/middleware/version.go b/api/server/middleware/version.go
index 390a7f0594..c8f9c7340b 100644
--- a/api/server/middleware/version.go
+++ b/api/server/middleware/version.go
@@ -5,7 +5,6 @@ import (
"net/http"
"runtime"
- "github.com/docker/docker/api/errors"
"github.com/docker/docker/api/types/versions"
"golang.org/x/net/context"
)
@@ -28,6 +27,16 @@ func NewVersionMiddleware(s, d, m string) VersionMiddleware {
}
}
+type versionUnsupportedError struct {
+ version, minVersion string
+}
+
+func (e versionUnsupportedError) Error() string {
+ return fmt.Sprintf("client version %s is too old. Minimum supported API version is %s, please upgrade your client to a newer version", e.version, e.minVersion)
+}
+
+func (e versionUnsupportedError) InvalidParameter() {}
+
// WrapHandler returns a new handler function wrapping the previous one in the request chain.
func (v VersionMiddleware) WrapHandler(handler func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error) func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
@@ -37,7 +46,7 @@ func (v VersionMiddleware) WrapHandler(handler func(ctx context.Context, w http.
}
if versions.LessThan(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))
+ return versionUnsupportedError{apiVersion, v.minVersion}
}
header := fmt.Sprintf("Docker/%s (%s)", v.serverVersion, runtime.GOOS)