diff --git a/api/server/router/container/container_routes.go b/api/server/router/container/container_routes.go index 5a2883324c..524d9f197f 100644 --- a/api/server/router/container/container_routes.go +++ b/api/server/router/container/container_routes.go @@ -721,7 +721,7 @@ func (s *containerRouter) postContainersPrune(ctx context.Context, w http.Respon pruneFilters, err := filters.FromJSON(r.Form.Get("filters")) if err != nil { - return errdefs.InvalidParameter(err) + return err } pruneReport, err := s.backend.ContainersPrune(ctx, pruneFilters) diff --git a/api/server/router/network/network_routes.go b/api/server/router/network/network_routes.go index 96b65c3777..69fc26a129 100644 --- a/api/server/router/network/network_routes.go +++ b/api/server/router/network/network_routes.go @@ -30,7 +30,7 @@ func (n *networkRouter) getNetworksList(ctx context.Context, w http.ResponseWrit } if err := network.ValidateFilters(filter); err != nil { - return errdefs.InvalidParameter(err) + return err } var list []types.NetworkResource diff --git a/api/server/router/swarm/cluster_routes.go b/api/server/router/swarm/cluster_routes.go index 6ab097b4fd..0751617450 100644 --- a/api/server/router/swarm/cluster_routes.go +++ b/api/server/router/swarm/cluster_routes.go @@ -164,7 +164,7 @@ func (sr *swarmRouter) getServices(ctx context.Context, w http.ResponseWriter, r } filter, err := filters.FromJSON(r.Form.Get("filters")) if err != nil { - return errdefs.InvalidParameter(err) + return err } // the status query parameter is only support in API versions >= 1.41. If diff --git a/api/server/router/volume/volume_routes.go b/api/server/router/volume/volume_routes.go index 4fa4cb53aa..e08e724579 100644 --- a/api/server/router/volume/volume_routes.go +++ b/api/server/router/volume/volume_routes.go @@ -21,7 +21,7 @@ func (v *volumeRouter) getVolumesList(ctx context.Context, w http.ResponseWriter filters, err := filters.FromJSON(r.Form.Get("filters")) if err != nil { - return errdefs.InvalidParameter(errors.Wrap(err, "error reading volume filters")) + return errors.Wrap(err, "error reading volume filters") } volumes, warnings, err := v.backend.List(ctx, filters) if err != nil { diff --git a/api/types/filters/parse.go b/api/types/filters/parse.go index 4bc91cffd6..6a2139adb3 100644 --- a/api/types/filters/parse.go +++ b/api/types/filters/parse.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/docker/docker/api/types/versions" + "github.com/pkg/errors" ) // Args stores a mapping of keys to a set of multiple values. @@ -97,7 +98,7 @@ func FromJSON(p string) (Args, error) { // Fallback to parsing arguments in the legacy slice format deprecated := map[string][]string{} if legacyErr := json.Unmarshal(raw, &deprecated); legacyErr != nil { - return args, err + return args, invalidFilter{errors.Wrap(err, "invalid filter")} } args.fields = deprecatedArgs(deprecated) @@ -247,10 +248,10 @@ func (args Args) Contains(field string) bool { return ok } -type invalidFilter string +type invalidFilter struct{ error } func (e invalidFilter) Error() string { - return "Invalid filter '" + string(e) + "'" + return e.error.Error() } func (invalidFilter) InvalidParameter() {} @@ -260,7 +261,7 @@ func (invalidFilter) InvalidParameter() {} func (args Args) Validate(accepted map[string]bool) error { for name := range args.fields { if !accepted[name] { - return invalidFilter(name) + return invalidFilter{errors.New("invalid filter '" + name + "'")} } } return nil diff --git a/api/types/filters/parse_test.go b/api/types/filters/parse_test.go index 4b68c9685d..fe7958cf5b 100644 --- a/api/types/filters/parse_test.go +++ b/api/types/filters/parse_test.go @@ -69,9 +69,14 @@ func TestFromJSON(t *testing.T) { } for _, invalid := range invalids { - if _, err := FromJSON(invalid); err == nil { + _, err := FromJSON(invalid) + if err == nil { t.Fatalf("Expected an error with %v, got nothing", invalid) } + var invalidFilterError invalidFilter + if !errors.As(err, &invalidFilterError) { + t.Fatalf("Expected an invalidFilter error, got %T", err) + } } for expectedArgs, matchers := range valid { @@ -327,9 +332,14 @@ func TestValidate(t *testing.T) { } f.Add("bogus", "running") - if err := f.Validate(valid); err == nil { + err := f.Validate(valid) + if err == nil { t.Fatal("Expected to return an error, got nil") } + var invalidFilterError invalidFilter + if !errors.As(err, &invalidFilterError) { + t.Fatalf("Expected an invalidFilter error, got %T", err) + } } func TestWalkValues(t *testing.T) {