Browse Source

libnet: Replace BadRequest with InvalidParameter

InvalidParameter is now compatible with errdefs.InvalidParameter. Thus,
these errors will now return a 400 status code instead of a 500.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
Albin Kerouanton 1 year ago
parent
commit
42d34e40f9

+ 2 - 2
libnetwork/controller.go

@@ -845,7 +845,7 @@ func (c *Controller) NetworkByID(id string) (*Network, error) {
 // NewSandbox creates a new sandbox for containerID.
 func (c *Controller) NewSandbox(containerID string, options ...SandboxOption) (*Sandbox, error) {
 	if containerID == "" {
-		return nil, types.BadRequestErrorf("invalid container ID")
+		return nil, types.InvalidParameterErrorf("invalid container ID")
 	}
 
 	var sb *Sandbox
@@ -1105,7 +1105,7 @@ func (c *Controller) getIPAMDriver(name string) (ipamapi.Ipam, *ipamapi.Capabili
 		// Now that we resolved the plugin, try again looking up the registry
 		id, cap = c.ipamRegistry.IPAM(name)
 		if id == nil {
-			return nil, nil, types.BadRequestErrorf("invalid ipam driver: %q", name)
+			return nil, nil, types.InvalidParameterErrorf("invalid ipam driver: %q", name)
 		}
 	}
 

+ 3 - 3
libnetwork/datastore/datastore.go

@@ -221,13 +221,13 @@ func (ds *Store) PutObjectAtomic(kvObject KVObject) error {
 	defer ds.mu.Unlock()
 
 	if kvObject == nil {
-		return types.BadRequestErrorf("invalid KV Object : nil")
+		return types.InvalidParameterErrorf("invalid KV Object : nil")
 	}
 
 	kvObjValue := kvObject.Value()
 
 	if kvObjValue == nil {
-		return types.BadRequestErrorf("invalid KV Object with a nil Value for key %s", Key(kvObject.Key()...))
+		return types.InvalidParameterErrorf("invalid KV Object with a nil Value for key %s", Key(kvObject.Key()...))
 	}
 
 	if kvObject.Skip() {
@@ -375,7 +375,7 @@ func (ds *Store) DeleteObjectAtomic(kvObject KVObject) error {
 	defer ds.mu.Unlock()
 
 	if kvObject == nil {
-		return types.BadRequestErrorf("invalid KV Object : nil")
+		return types.InvalidParameterErrorf("invalid KV Object : nil")
 	}
 
 	previous := &store.KVPair{Key: Key(kvObject.Key()...), LastIndex: kvObject.Index()}

+ 4 - 4
libnetwork/datastore/mockstore_test.go

@@ -62,14 +62,14 @@ func (s *MockStore) AtomicPut(key string, newValue []byte, previous *store.KVPai
 
 	if previous == nil {
 		if mData != nil {
-			return nil, types.BadRequestErrorf("atomic put failed because key exists")
+			return nil, types.InvalidParameterErrorf("atomic put failed because key exists")
 		} // Else OK.
 	} else {
 		if mData == nil {
-			return nil, types.BadRequestErrorf("atomic put failed because key exists")
+			return nil, types.InvalidParameterErrorf("atomic put failed because key exists")
 		}
 		if mData != nil && mData.Index != previous.LastIndex {
-			return nil, types.BadRequestErrorf("atomic put failed due to mismatched Index")
+			return nil, types.InvalidParameterErrorf("atomic put failed due to mismatched Index")
 		} // Else OK.
 	}
 	if err := s.Put(key, newValue); err != nil {
@@ -83,7 +83,7 @@ func (s *MockStore) AtomicPut(key string, newValue []byte, previous *store.KVPai
 func (s *MockStore) AtomicDelete(key string, previous *store.KVPair) error {
 	mData := s.db[key]
 	if mData != nil && mData.Index != previous.LastIndex {
-		return types.BadRequestErrorf("atomic delete failed due to mismatched Index")
+		return types.InvalidParameterErrorf("atomic delete failed due to mismatched Index")
 	}
 	delete(s.db, key)
 	return nil

+ 6 - 6
libnetwork/driverapi/ipamdata.go

@@ -68,26 +68,26 @@ func (i *IPAMData) UnmarshalJSON(data []byte) error {
 func (i *IPAMData) Validate() error {
 	var isV6 bool
 	if i.Pool == nil {
-		return types.BadRequestErrorf("invalid pool")
+		return types.InvalidParameterErrorf("invalid pool")
 	}
 	if i.Gateway == nil {
-		return types.BadRequestErrorf("invalid gateway address")
+		return types.InvalidParameterErrorf("invalid gateway address")
 	}
 	isV6 = i.IsV6()
 	if isV6 && i.Gateway.IP.To4() != nil || !isV6 && i.Gateway.IP.To4() == nil {
-		return types.BadRequestErrorf("incongruent ip versions for pool and gateway")
+		return types.InvalidParameterErrorf("incongruent ip versions for pool and gateway")
 	}
 	for k, sip := range i.AuxAddresses {
 		if isV6 && sip.IP.To4() != nil || !isV6 && sip.IP.To4() == nil {
-			return types.BadRequestErrorf("incongruent ip versions for pool and secondary ip address %s", k)
+			return types.InvalidParameterErrorf("incongruent ip versions for pool and secondary ip address %s", k)
 		}
 	}
 	if !i.Pool.Contains(i.Gateway.IP) {
-		return types.BadRequestErrorf("invalid gateway address (%s) does not belong to the pool (%s)", i.Gateway, i.Pool)
+		return types.InvalidParameterErrorf("invalid gateway address (%s) does not belong to the pool (%s)", i.Gateway, i.Pool)
 	}
 	for k, sip := range i.AuxAddresses {
 		if !i.Pool.Contains(sip.IP) {
-			return types.BadRequestErrorf("invalid secondary address %s (%s) does not belong to the pool (%s)", k, i.Gateway, i.Pool)
+			return types.InvalidParameterErrorf("invalid secondary address %s (%s) does not belong to the pool (%s)", k, i.Gateway, i.Pool)
 		}
 	}
 	return nil

+ 8 - 8
libnetwork/drivers/bridge/bridge_linux.go

@@ -277,7 +277,7 @@ func (c *networkConfiguration) fromLabels(labels map[string]string) error {
 }
 
 func parseErr(label, value, errString string) error {
-	return types.BadRequestErrorf("failed to parse %s value: %v (%s)", label, value, errString)
+	return types.InvalidParameterErrorf("failed to parse %s value: %v (%s)", label, value, errString)
 }
 
 func (n *bridgeNetwork) registerIptCleanFunc(clean iptableCleanFunc) {
@@ -289,7 +289,7 @@ func (n *bridgeNetwork) getDriverChains(version iptables.IPVersion) (*iptables.C
 	defer n.Unlock()
 
 	if n.driver == nil {
-		return nil, nil, nil, nil, types.BadRequestErrorf("no driver found")
+		return nil, nil, nil, nil, types.InvalidParameterErrorf("no driver found")
 	}
 
 	if version == iptables.IPv6 {
@@ -445,7 +445,7 @@ func (d *driver) getNetwork(id string) (*bridgeNetwork, error) {
 	defer d.Unlock()
 
 	if id == "" {
-		return nil, types.BadRequestErrorf("invalid network id: %s", id)
+		return nil, types.InvalidParameterErrorf("invalid network id: %s", id)
 	}
 
 	if nw, ok := d.networks[id]; ok {
@@ -476,7 +476,7 @@ func parseNetworkGenericOptions(data interface{}) (*networkConfiguration, error)
 			config = opaqueConfig.(*networkConfiguration)
 		}
 	default:
-		err = types.BadRequestErrorf("do not recognize network configuration format: %T", opt)
+		err = types.InvalidParameterErrorf("do not recognize network configuration format: %T", opt)
 	}
 
 	return config, err
@@ -488,7 +488,7 @@ func (c *networkConfiguration) processIPAM(id string, ipamV4Data, ipamV6Data []d
 	}
 
 	if len(ipamV4Data) == 0 {
-		return types.BadRequestErrorf("bridge network %s requires ipv4 configuration", id)
+		return types.InvalidParameterErrorf("bridge network %s requires ipv4 configuration", id)
 	}
 
 	if ipamV4Data[0].Gateway != nil {
@@ -592,7 +592,7 @@ func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (s
 // Create a new network using bridge plugin
 func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
 	if len(ipV4Data) == 0 || ipV4Data[0].Pool.String() == "0.0.0.0/0" {
-		return types.BadRequestErrorf("ipv4 pool is empty")
+		return types.InvalidParameterErrorf("ipv4 pool is empty")
 	}
 	// Sanity checks
 	d.Lock()
@@ -1479,7 +1479,7 @@ func parseConnectivityOptions(cOptions map[string]interface{}) (*connectivityCon
 		if pb, ok := opt.([]types.PortBinding); ok {
 			cc.PortBindings = pb
 		} else {
-			return nil, types.BadRequestErrorf("Invalid port mapping data in connectivity configuration: %v", opt)
+			return nil, types.InvalidParameterErrorf("Invalid port mapping data in connectivity configuration: %v", opt)
 		}
 	}
 
@@ -1487,7 +1487,7 @@ func parseConnectivityOptions(cOptions map[string]interface{}) (*connectivityCon
 		if ports, ok := opt.([]types.TransportPort); ok {
 			cc.ExposedPorts = ports
 		} else {
-			return nil, types.BadRequestErrorf("Invalid exposed ports data in connectivity configuration: %v", opt)
+			return nil, types.InvalidParameterErrorf("Invalid exposed ports data in connectivity configuration: %v", opt)
 		}
 	}
 

+ 2 - 2
libnetwork/drivers/bridge/bridge_linux_test.go

@@ -574,7 +574,7 @@ func (i *testInterface) SetMacAddress(mac net.HardwareAddr) error {
 		return types.ForbiddenErrorf("endpoint interface MAC address present (%s). Cannot be modified with %s.", i.mac, mac)
 	}
 	if mac == nil {
-		return types.BadRequestErrorf("tried to set nil MAC address to endpoint interface")
+		return types.InvalidParameterErrorf("tried to set nil MAC address to endpoint interface")
 	}
 	i.mac = types.GetMacCopy(mac)
 	return nil
@@ -582,7 +582,7 @@ func (i *testInterface) SetMacAddress(mac net.HardwareAddr) error {
 
 func (i *testInterface) SetIPAddress(address *net.IPNet) error {
 	if address.IP == nil {
-		return types.BadRequestErrorf("tried to set nil IP address to endpoint interface")
+		return types.InvalidParameterErrorf("tried to set nil IP address to endpoint interface")
 	}
 	if address.IP.To4() == nil {
 		return setAddress(&i.addrv6, address)

+ 12 - 12
libnetwork/drivers/bridge/errors.go

@@ -41,8 +41,8 @@ func (eiec *ErrInvalidEndpointConfig) Error() string {
 	return "trying to create an endpoint with an invalid endpoint configuration"
 }
 
-// BadRequest denotes the type of this error
-func (eiec *ErrInvalidEndpointConfig) BadRequest() {}
+// InvalidParameter denotes the type of this error
+func (eiec *ErrInvalidEndpointConfig) InvalidParameter() {}
 
 // ErrNetworkExists error is returned when a network already exists and another network is created.
 type ErrNetworkExists struct{}
@@ -81,8 +81,8 @@ func (eig *ErrInvalidGateway) Error() string {
 	return "default gateway ip must be part of the network"
 }
 
-// BadRequest denotes the type of this error
-func (eig *ErrInvalidGateway) BadRequest() {}
+// InvalidParameter denotes the type of this error
+func (eig *ErrInvalidGateway) InvalidParameter() {}
 
 // ErrInvalidContainerSubnet is returned when the container subnet (FixedCIDR) is not valid.
 type ErrInvalidContainerSubnet struct{}
@@ -91,8 +91,8 @@ func (eis *ErrInvalidContainerSubnet) Error() string {
 	return "container subnet must be a subset of bridge network"
 }
 
-// BadRequest denotes the type of this error
-func (eis *ErrInvalidContainerSubnet) BadRequest() {}
+// InvalidParameter denotes the type of this error
+func (eis *ErrInvalidContainerSubnet) InvalidParameter() {}
 
 // ErrInvalidMtu is returned when the user provided MTU is not valid.
 type ErrInvalidMtu int
@@ -101,8 +101,8 @@ func (eim ErrInvalidMtu) Error() string {
 	return fmt.Sprintf("invalid MTU number: %d", int(eim))
 }
 
-// BadRequest denotes the type of this error
-func (eim ErrInvalidMtu) BadRequest() {}
+// InvalidParameter denotes the type of this error
+func (eim ErrInvalidMtu) InvalidParameter() {}
 
 // ErrUnsupportedAddressType is returned when the specified address type is not supported.
 type ErrUnsupportedAddressType string
@@ -111,8 +111,8 @@ func (uat ErrUnsupportedAddressType) Error() string {
 	return fmt.Sprintf("unsupported address type: %s", string(uat))
 }
 
-// BadRequest denotes the type of this error
-func (uat ErrUnsupportedAddressType) BadRequest() {}
+// InvalidParameter denotes the type of this error
+func (uat ErrUnsupportedAddressType) InvalidParameter() {}
 
 // ActiveEndpointsError is returned when there are
 // still active endpoints in the network being deleted.
@@ -144,8 +144,8 @@ func (ieie InvalidEndpointIDError) Error() string {
 	return fmt.Sprintf("invalid endpoint id: %s", string(ieie))
 }
 
-// BadRequest denotes the type of this error
-func (ieie InvalidEndpointIDError) BadRequest() {}
+// InvalidParameter denotes the type of this error
+func (ieie InvalidEndpointIDError) InvalidParameter() {}
 
 // EndpointNotFoundError is returned when the no endpoint
 // with the passed endpoint id is found.

+ 1 - 1
libnetwork/drivers/ipvlan/ipvlan_network.go

@@ -227,7 +227,7 @@ func parseNetworkGenericOptions(data interface{}) (*configuration, error) {
 		}
 		return opaqueConfig.(*configuration), nil
 	default:
-		return nil, types.BadRequestErrorf("unrecognized network configuration format: %v", opt)
+		return nil, types.InvalidParameterErrorf("unrecognized network configuration format: %v", opt)
 	}
 }
 

+ 1 - 1
libnetwork/drivers/ipvlan/ipvlan_state.go

@@ -94,7 +94,7 @@ func (d *driver) getNetwork(id string) (*network, error) {
 	d.Lock()
 	defer d.Unlock()
 	if id == "" {
-		return nil, types.BadRequestErrorf("invalid network id: %s", id)
+		return nil, types.InvalidParameterErrorf("invalid network id: %s", id)
 	}
 
 	if nw, ok := d.networks[id]; ok {

+ 1 - 1
libnetwork/drivers/macvlan/macvlan_network.go

@@ -206,7 +206,7 @@ func parseNetworkGenericOptions(data interface{}) (*configuration, error) {
 		}
 		return opaqueConfig.(*configuration), nil
 	default:
-		return nil, types.BadRequestErrorf("unrecognized network configuration format: %v", opt)
+		return nil, types.InvalidParameterErrorf("unrecognized network configuration format: %v", opt)
 	}
 }
 

+ 1 - 1
libnetwork/drivers/macvlan/macvlan_state.go

@@ -93,7 +93,7 @@ func (d *driver) getNetwork(id string) (*network, error) {
 	d.Lock()
 	defer d.Unlock()
 	if id == "" {
-		return nil, types.BadRequestErrorf("invalid network id: %s", id)
+		return nil, types.InvalidParameterErrorf("invalid network id: %s", id)
 	}
 	if nw, ok := d.networks[id]; ok {
 		return nw, nil

+ 2 - 2
libnetwork/drivers/overlay/encryption.go

@@ -519,12 +519,12 @@ func (d *driver) updateKeys(newKey, primary, pruneKey *key) error {
 	if (newKey != nil && newIdx == -1) ||
 		(primary != nil && priIdx == -1) ||
 		(pruneKey != nil && delIdx == -1) {
-		return types.BadRequestErrorf("cannot find proper key indices while processing key update:"+
+		return types.InvalidParameterErrorf("cannot find proper key indices while processing key update:"+
 			"(newIdx,priIdx,delIdx):(%d, %d, %d)", newIdx, priIdx, delIdx)
 	}
 
 	if priIdx != -1 && priIdx == delIdx {
-		return types.BadRequestErrorf("attempting to both make a key (index %d) primary and delete it", priIdx)
+		return types.InvalidParameterErrorf("attempting to both make a key (index %d) primary and delete it", priIdx)
 	}
 
 	d.secMapWalk(func(rIPs string, spis []*spi) ([]*spi, bool) {

+ 1 - 1
libnetwork/drivers/overlay/ov_network.go

@@ -83,7 +83,7 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo d
 		return fmt.Errorf("invalid network id")
 	}
 	if len(ipV4Data) == 0 || ipV4Data[0].Pool.String() == "0.0.0.0/0" {
-		return types.BadRequestErrorf("ipv4 pool is empty")
+		return types.InvalidParameterErrorf("ipv4 pool is empty")
 	}
 
 	// Since we perform lazy configuration make sure we try

+ 2 - 2
libnetwork/drivers/remote/driver_test.go

@@ -128,7 +128,7 @@ func (test *testEndpoint) SetMacAddress(mac net.HardwareAddr) error {
 		return types.ForbiddenErrorf("endpoint interface MAC address present (%s). Cannot be modified with %s.", test.macAddress, mac)
 	}
 	if mac == nil {
-		return types.BadRequestErrorf("tried to set nil MAC address to endpoint interface")
+		return types.InvalidParameterErrorf("tried to set nil MAC address to endpoint interface")
 	}
 	test.macAddress = mac.String()
 	return nil
@@ -136,7 +136,7 @@ func (test *testEndpoint) SetMacAddress(mac net.HardwareAddr) error {
 
 func (test *testEndpoint) SetIPAddress(address *net.IPNet) error {
 	if address.IP == nil {
-		return types.BadRequestErrorf("tried to set nil IP address to endpoint interface")
+		return types.InvalidParameterErrorf("tried to set nil IP address to endpoint interface")
 	}
 	if address.IP.To4() == nil {
 		return setAddress(&test.addressIPv6, address)

+ 1 - 1
libnetwork/drivers/windows/overlay/ov_network_windows.go

@@ -76,7 +76,7 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo d
 	}
 
 	if len(ipV4Data) == 0 || ipV4Data[0].Pool.String() == "0.0.0.0/0" {
-		return types.BadRequestErrorf("ipv4 pool is empty")
+		return types.InvalidParameterErrorf("ipv4 pool is empty")
 	}
 
 	staleNetworks = make([]string, 0)

+ 2 - 2
libnetwork/drivers/windows/windows.go

@@ -199,7 +199,7 @@ func (d *driver) parseNetworkOptions(id string, genericOptions map[string]string
 			config.MacPools = make([]hcsshim.MacPool, 0)
 			s := strings.Split(value, ",")
 			if len(s)%2 != 0 {
-				return nil, types.BadRequestErrorf("Invalid mac pool. You must specify both a start range and an end range")
+				return nil, types.InvalidParameterErrorf("Invalid mac pool. You must specify both a start range and an end range")
 			}
 			for i := 0; i < len(s)-1; i += 2 {
 				config.MacPools = append(config.MacPools, hcsshim.MacPool{
@@ -242,7 +242,7 @@ func (c *networkConfiguration) processIPAM(id string, ipamV4Data, ipamV6Data []d
 	}
 
 	if len(ipamV4Data) == 0 {
-		return types.BadRequestErrorf("network %s requires ipv4 configuration", id)
+		return types.InvalidParameterErrorf("network %s requires ipv4 configuration", id)
 	}
 
 	return nil

+ 2 - 2
libnetwork/drivers/windows/windows_test.go

@@ -103,7 +103,7 @@ func (test *testEndpoint) SetMacAddress(mac net.HardwareAddr) error {
 	}
 
 	if mac == nil {
-		return types.BadRequestErrorf("tried to set nil MAC address to endpoint interface")
+		return types.InvalidParameterErrorf("tried to set nil MAC address to endpoint interface")
 	}
 	test.macAddress = mac.String()
 	return nil
@@ -111,7 +111,7 @@ func (test *testEndpoint) SetMacAddress(mac net.HardwareAddr) error {
 
 func (test *testEndpoint) SetIPAddress(address *net.IPNet) error {
 	if address.IP == nil {
-		return types.BadRequestErrorf("tried to set nil IP address to endpoint interface")
+		return types.InvalidParameterErrorf("tried to set nil IP address to endpoint interface")
 	}
 
 	test.address = address.String()

+ 3 - 3
libnetwork/endpoint.go

@@ -397,7 +397,7 @@ func (ep *Endpoint) getNetworkFromStore() (*Network, error) {
 // the network resources allocated for the endpoint.
 func (ep *Endpoint) Join(sb *Sandbox, options ...EndpointOption) error {
 	if sb == nil || sb.ID() == "" || sb.Key() == "" {
-		return types.BadRequestErrorf("invalid Sandbox passed to endpoint join: %v", sb)
+		return types.InvalidParameterErrorf("invalid Sandbox passed to endpoint join: %v", sb)
 	}
 
 	sb.joinLeaveStart()
@@ -658,7 +658,7 @@ func (ep *Endpoint) hasInterface(iName string) bool {
 // Leave detaches the network resources populated in the sandbox.
 func (ep *Endpoint) Leave(sb *Sandbox, options ...EndpointOption) error {
 	if sb == nil || sb.ID() == "" || sb.Key() == "" {
-		return types.BadRequestErrorf("invalid Sandbox passed to endpoint leave: %v", sb)
+		return types.InvalidParameterErrorf("invalid Sandbox passed to endpoint leave: %v", sb)
 	}
 
 	sb.joinLeaveStart()
@@ -1112,7 +1112,7 @@ func (ep *Endpoint) assignAddressVersion(ipVer int, ipam ipamapi.Ipam) error {
 		}
 	}
 	if progAdd != nil {
-		return types.BadRequestErrorf("Invalid address %s: It does not belong to any of this network's subnets", prefAdd)
+		return types.InvalidParameterErrorf("Invalid address %s: It does not belong to any of this network's subnets", prefAdd)
 	}
 	return fmt.Errorf("no available IPv%d addresses on this network's address pools: %s (%s)", ipVer, n.Name(), n.ID())
 }

+ 2 - 2
libnetwork/endpoint_info.go

@@ -239,7 +239,7 @@ func (epi *endpointInterface) SetMacAddress(mac net.HardwareAddr) error {
 		return types.ForbiddenErrorf("endpoint interface MAC address present (%s). Cannot be modified with %s.", epi.mac, mac)
 	}
 	if mac == nil {
-		return types.BadRequestErrorf("tried to set nil MAC address to endpoint interface")
+		return types.InvalidParameterErrorf("tried to set nil MAC address to endpoint interface")
 	}
 	epi.mac = types.GetMacCopy(mac)
 	return nil
@@ -247,7 +247,7 @@ func (epi *endpointInterface) SetMacAddress(mac net.HardwareAddr) error {
 
 func (epi *endpointInterface) SetIPAddress(address *net.IPNet) error {
 	if address.IP == nil {
-		return types.BadRequestErrorf("tried to set nil IP address to endpoint interface")
+		return types.InvalidParameterErrorf("tried to set nil IP address to endpoint interface")
 	}
 	if address.IP.To4() == nil {
 		return setAddress(&epi.addrv6, address)

+ 4 - 4
libnetwork/error.go

@@ -32,8 +32,8 @@ func (ii ErrInvalidID) Error() string {
 	return fmt.Sprintf("invalid id: %s", string(ii))
 }
 
-// BadRequest denotes the type of this error
-func (ii ErrInvalidID) BadRequest() {}
+// InvalidParameter denotes the type of this error
+func (ii ErrInvalidID) InvalidParameter() {}
 
 // ErrInvalidName is returned when a query-by-name or resource create method is
 // invoked with an empty name parameter
@@ -43,8 +43,8 @@ func (in ErrInvalidName) Error() string {
 	return fmt.Sprintf("invalid name: %s", string(in))
 }
 
-// BadRequest denotes the type of this error
-func (in ErrInvalidName) BadRequest() {}
+// InvalidParameter denotes the type of this error
+func (in ErrInvalidName) InvalidParameter() {}
 
 // NetworkNameError is returned when a network with the same name already exists.
 type NetworkNameError string

+ 2 - 2
libnetwork/errors_test.go

@@ -10,9 +10,9 @@ func TestErrorInterfaces(t *testing.T) {
 	badRequestErrorList := []error{ErrInvalidID(""), ErrInvalidName("")}
 	for _, err := range badRequestErrorList {
 		switch u := err.(type) {
-		case types.BadRequestError:
+		case types.InvalidParameterError:
 		default:
-			t.Errorf("Failed to detect err %v is of type BadRequestError. Got type: %T", err, u)
+			t.Errorf("Failed to detect err %v is of type InvalidParameterError. Got type: %T", err, u)
 		}
 	}
 

+ 7 - 7
libnetwork/ipam/allocator.go

@@ -119,7 +119,7 @@ func (a *Allocator) ReleasePool(poolID string) error {
 	log.G(context.TODO()).Debugf("ReleasePool(%s)", poolID)
 	k, err := PoolIDFromString(poolID)
 	if err != nil {
-		return types.BadRequestErrorf("invalid pool id: %s", poolID)
+		return types.InvalidParameterErrorf("invalid pool id: %s", poolID)
 	}
 
 	aSpace, err := a.getAddrSpace(k.AddressSpace)
@@ -139,7 +139,7 @@ func (a *Allocator) getAddrSpace(as string) (*addrSpace, error) {
 	case globalAddressSpace:
 		return a.global, nil
 	}
-	return nil, types.BadRequestErrorf("cannot find address space %s", as)
+	return nil, types.InvalidParameterErrorf("cannot find address space %s", as)
 }
 
 func newPoolData(pool netip.Prefix) *PoolData {
@@ -228,7 +228,7 @@ func (a *Allocator) RequestAddress(poolID string, prefAddress net.IP, opts map[s
 	log.G(context.TODO()).Debugf("RequestAddress(%s, %v, %v)", poolID, prefAddress, opts)
 	k, err := PoolIDFromString(poolID)
 	if err != nil {
-		return nil, nil, types.BadRequestErrorf("invalid pool id: %s", poolID)
+		return nil, nil, types.InvalidParameterErrorf("invalid pool id: %s", poolID)
 	}
 
 	aSpace, err := a.getAddrSpace(k.AddressSpace)
@@ -240,7 +240,7 @@ func (a *Allocator) RequestAddress(poolID string, prefAddress net.IP, opts map[s
 		var ok bool
 		pref, ok = netip.AddrFromSlice(prefAddress)
 		if !ok {
-			return nil, nil, types.BadRequestErrorf("invalid preferred address: %v", prefAddress)
+			return nil, nil, types.InvalidParameterErrorf("invalid preferred address: %v", prefAddress)
 		}
 	}
 	p, err := aSpace.requestAddress(k.Subnet, k.ChildSubnet, pref.Unmap(), opts)
@@ -288,7 +288,7 @@ func (a *Allocator) ReleaseAddress(poolID string, address net.IP) error {
 	log.G(context.TODO()).Debugf("ReleaseAddress(%s, %v)", poolID, address)
 	k, err := PoolIDFromString(poolID)
 	if err != nil {
-		return types.BadRequestErrorf("invalid pool id: %s", poolID)
+		return types.InvalidParameterErrorf("invalid pool id: %s", poolID)
 	}
 
 	aSpace, err := a.getAddrSpace(k.AddressSpace)
@@ -298,7 +298,7 @@ func (a *Allocator) ReleaseAddress(poolID string, address net.IP) error {
 
 	addr, ok := netip.AddrFromSlice(address)
 	if !ok {
-		return types.BadRequestErrorf("invalid address: %v", address)
+		return types.InvalidParameterErrorf("invalid address: %v", address)
 	}
 
 	return aSpace.releaseAddress(k.Subnet, k.ChildSubnet, addr.Unmap())
@@ -319,7 +319,7 @@ func (aSpace *addrSpace) releaseAddress(nw, sub netip.Prefix, address netip.Addr
 	}
 
 	if !address.IsValid() {
-		return types.BadRequestErrorf("invalid address")
+		return types.InvalidParameterErrorf("invalid address")
 	}
 
 	if !nw.Contains(address) {

+ 4 - 4
libnetwork/ipam/structures.go

@@ -47,22 +47,22 @@ type addrSpace struct {
 // reading it from the given string.
 func PoolIDFromString(str string) (pID PoolID, err error) {
 	if str == "" {
-		return pID, types.BadRequestErrorf("invalid string form for subnetkey: %s", str)
+		return pID, types.InvalidParameterErrorf("invalid string form for subnetkey: %s", str)
 	}
 
 	p := strings.Split(str, "/")
 	if len(p) != 3 && len(p) != 5 {
-		return pID, types.BadRequestErrorf("invalid string form for subnetkey: %s", str)
+		return pID, types.InvalidParameterErrorf("invalid string form for subnetkey: %s", str)
 	}
 	pID.AddressSpace = p[0]
 	pID.Subnet, err = netip.ParsePrefix(p[1] + "/" + p[2])
 	if err != nil {
-		return pID, types.BadRequestErrorf("%v", err)
+		return pID, types.InvalidParameterErrorf("invalid string form for subnetkey: %s", str)
 	}
 	if len(p) == 5 {
 		pID.ChildSubnet, err = netip.ParsePrefix(p[3] + "/" + p[4])
 		if err != nil {
-			return pID, types.BadRequestErrorf("%v", err)
+			return pID, types.InvalidParameterErrorf("invalid string form for subnetkey: %s", str)
 		}
 	}
 

+ 5 - 5
libnetwork/ipamapi/contract.go

@@ -29,15 +29,15 @@ type Registerer interface {
 
 // Well-known errors returned by IPAM
 var (
-	ErrInvalidAddressSpace = types.BadRequestErrorf("Invalid Address Space")
-	ErrInvalidPool         = types.BadRequestErrorf("Invalid Address Pool")
-	ErrInvalidSubPool      = types.BadRequestErrorf("Invalid Address SubPool")
+	ErrInvalidAddressSpace = types.InvalidParameterErrorf("Invalid Address Space")
+	ErrInvalidPool         = types.InvalidParameterErrorf("Invalid Address Pool")
+	ErrInvalidSubPool      = types.InvalidParameterErrorf("Invalid Address SubPool")
 	ErrNoAvailableIPs      = types.NoServiceErrorf("No available addresses on this pool")
 	ErrNoIPReturned        = types.NoServiceErrorf("No address returned")
 	ErrIPAlreadyAllocated  = types.ForbiddenErrorf("Address already in use")
-	ErrIPOutOfRange        = types.BadRequestErrorf("Requested address is out of range")
+	ErrIPOutOfRange        = types.InvalidParameterErrorf("Requested address is out of range")
 	ErrPoolOverlap         = types.ForbiddenErrorf("Pool overlaps with other one on this address space")
-	ErrBadPool             = types.BadRequestErrorf("Address space does not contain specified address pool")
+	ErrBadPool             = types.InvalidParameterErrorf("Address space does not contain specified address pool")
 )
 
 // Ipam represents the interface the IPAM service plugins must implement

+ 6 - 6
libnetwork/ipams/null/null.go

@@ -25,16 +25,16 @@ func (a *allocator) GetDefaultAddressSpaces() (string, string, error) {
 
 func (a *allocator) RequestPool(addressSpace, requestedPool, requestedSubPool string, _ map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) {
 	if addressSpace != defaultAddressSpace {
-		return "", nil, nil, types.BadRequestErrorf("unknown address space: %s", addressSpace)
+		return "", nil, nil, types.InvalidParameterErrorf("unknown address space: %s", addressSpace)
 	}
 	if requestedPool != "" {
-		return "", nil, nil, types.BadRequestErrorf("null ipam driver does not handle specific address pool requests")
+		return "", nil, nil, types.InvalidParameterErrorf("null ipam driver does not handle specific address pool requests")
 	}
 	if requestedSubPool != "" {
-		return "", nil, nil, types.BadRequestErrorf("null ipam driver does not handle specific address subpool requests")
+		return "", nil, nil, types.InvalidParameterErrorf("null ipam driver does not handle specific address subpool requests")
 	}
 	if v6 {
-		return "", nil, nil, types.BadRequestErrorf("null ipam driver does not handle IPv6 address pool pool requests")
+		return "", nil, nil, types.InvalidParameterErrorf("null ipam driver does not handle IPv6 address pool pool requests")
 	}
 	return defaultPoolID, defaultPool, nil, nil
 }
@@ -45,14 +45,14 @@ func (a *allocator) ReleasePool(poolID string) error {
 
 func (a *allocator) RequestAddress(poolID string, ip net.IP, opts map[string]string) (*net.IPNet, map[string]string, error) {
 	if poolID != defaultPoolID {
-		return nil, nil, types.BadRequestErrorf("unknown pool id: %s", poolID)
+		return nil, nil, types.InvalidParameterErrorf("unknown pool id: %s", poolID)
 	}
 	return nil, nil, nil
 }
 
 func (a *allocator) ReleaseAddress(poolID string, ip net.IP) error {
 	if poolID != defaultPoolID {
-		return types.BadRequestErrorf("unknown pool id: %s", poolID)
+		return types.InvalidParameterErrorf("unknown pool id: %s", poolID)
 	}
 	return nil
 }

+ 4 - 4
libnetwork/libnetwork_linux_test.go

@@ -1091,7 +1091,7 @@ func TestContainerInvalidLeave(t *testing.T) {
 	if err = ep.Leave(nil); err == nil {
 		t.Fatalf("Expected to fail leave nil Sandbox")
 	}
-	if _, ok := err.(types.BadRequestError); !ok {
+	if _, ok := err.(types.InvalidParameterError); !ok {
 		t.Fatalf("Unexpected error type returned: %T. Desc: %s", err, err.Error())
 	}
 
@@ -1099,7 +1099,7 @@ func TestContainerInvalidLeave(t *testing.T) {
 	if err = ep.Leave(fsbx); err == nil {
 		t.Fatalf("Expected to fail leave with invalid Sandbox")
 	}
-	if _, ok := err.(types.BadRequestError); !ok {
+	if _, ok := err.(types.InvalidParameterError); !ok {
 		t.Fatalf("Unexpected error type returned: %T. Desc: %s", err, err.Error())
 	}
 }
@@ -1516,7 +1516,7 @@ func TestEndpointJoin(t *testing.T) {
 	if err == nil {
 		t.Fatalf("Expected to fail join with nil Sandbox")
 	}
-	if _, ok := err.(types.BadRequestError); !ok {
+	if _, ok := err.(types.InvalidParameterError); !ok {
 		t.Fatalf("Unexpected error type returned: %T", err)
 	}
 
@@ -1524,7 +1524,7 @@ func TestEndpointJoin(t *testing.T) {
 	if err = ep1.Join(fsbx); err == nil {
 		t.Fatalf("Expected to fail join with invalid Sandbox")
 	}
-	if _, ok := err.(types.BadRequestError); !ok {
+	if _, ok := err.(types.InvalidParameterError); !ok {
 		t.Fatalf("Unexpected error type returned: %T", err)
 	}
 

+ 4 - 4
libnetwork/network.go

@@ -87,7 +87,7 @@ type IpamConf struct {
 // Validate checks whether the configuration is valid
 func (c *IpamConf) Validate() error {
 	if c.Gateway != "" && nil == net.ParseIP(c.Gateway) {
-		return types.BadRequestErrorf("invalid gateway address %s in Ipam configuration", c.Gateway)
+		return types.InvalidParameterErrorf("invalid gateway address %s in Ipam configuration", c.Gateway)
 	}
 	return nil
 }
@@ -1136,7 +1136,7 @@ func (n *Network) createEndpoint(name string, options ...EndpointOption) (*Endpo
 
 	for _, llIPNet := range ep.Iface().LinkLocalAddresses() {
 		if !llIPNet.IP.IsLinkLocalUnicast() {
-			return nil, types.BadRequestErrorf("invalid link local IP address: %v", llIPNet.IP)
+			return nil, types.InvalidParameterErrorf("invalid link local IP address: %v", llIPNet.IP)
 		}
 	}
 
@@ -1602,7 +1602,7 @@ func (n *Network) ipamAllocateVersion(ipVer int, ipam ipamapi.Ipam) error {
 
 		if gws, ok := d.Meta[netlabel.Gateway]; ok {
 			if d.Gateway, err = types.ParseCIDR(gws); err != nil {
-				return types.BadRequestErrorf("failed to parse gateway address (%v) returned by ipam driver: %v", gws, err)
+				return types.InvalidParameterErrorf("failed to parse gateway address (%v) returned by ipam driver: %v", gws, err)
 			}
 		}
 
@@ -1625,7 +1625,7 @@ func (n *Network) ipamAllocateVersion(ipVer int, ipam ipamapi.Ipam) error {
 			d.IPAMData.AuxAddresses = make(map[string]*net.IPNet, len(cfg.AuxAddresses))
 			for k, v := range cfg.AuxAddresses {
 				if ip = net.ParseIP(v); ip == nil {
-					return types.BadRequestErrorf("non parsable secondary ip address (%s:%s) passed for network %s", k, v, n.Name())
+					return types.InvalidParameterErrorf("non parsable secondary ip address (%s:%s) passed for network %s", k, v, n.Name())
 				}
 				if !d.Pool.Contains(ip) {
 					return types.ForbiddenErrorf("auxiliary address: (%s:%s) must belong to the master pool: %s", k, v, d.Pool)

+ 1 - 1
libnetwork/sandbox.go

@@ -609,7 +609,7 @@ func (sb *Sandbox) SetKey(basePath string) error {
 	}()
 
 	if basePath == "" {
-		return types.BadRequestErrorf("invalid sandbox key")
+		return types.InvalidParameterErrorf("invalid sandbox key")
 	}
 
 	sb.mu.Lock()

+ 1 - 1
libnetwork/sandbox_externalkey_unix.go

@@ -169,7 +169,7 @@ func (c *Controller) processExternalKey(conn net.Conn) error {
 	search := SandboxContainerWalker(&sandbox, s.ContainerID)
 	c.WalkSandboxes(search)
 	if sandbox == nil {
-		return types.BadRequestErrorf("no sandbox present for %s", s.ContainerID)
+		return types.InvalidParameterErrorf("no sandbox present for %s", s.ContainerID)
 	}
 
 	return sandbox.SetKey(s.Key)

+ 8 - 14
libnetwork/types/types.go

@@ -8,6 +8,7 @@ import (
 	"strconv"
 	"strings"
 
+	"github.com/docker/docker/errdefs"
 	"github.com/ishidawataru/sctp"
 )
 
@@ -355,10 +356,10 @@ type MaskableError interface {
 	Maskable()
 }
 
-// BadRequestError is an interface for errors originated by a bad request
-type BadRequestError interface {
-	// BadRequest makes implementer into BadRequestError type
-	BadRequest()
+// InvalidParameterError is an interface for errors originated by a bad request
+type InvalidParameterError interface {
+	// InvalidParameter makes implementer into InvalidParameterError type
+	InvalidParameter()
 }
 
 // NotFoundError is an interface for errors raised because a needed resource is not available
@@ -395,9 +396,9 @@ type InternalError interface {
  * Well-known Error Formatters
  ******************************/
 
-// BadRequestErrorf creates an instance of BadRequestError
-func BadRequestErrorf(format string, params ...interface{}) error {
-	return badRequest(fmt.Sprintf(format, params...))
+// InvalidParameterErrorf creates an instance of InvalidParameterError
+func InvalidParameterErrorf(format string, params ...interface{}) error {
+	return errdefs.InvalidParameter(fmt.Errorf(format, params...))
 }
 
 // NotFoundErrorf creates an instance of NotFoundError
@@ -433,13 +434,6 @@ func InternalMaskableErrorf(format string, params ...interface{}) error {
 /***********************
  * Internal Error Types
  ***********************/
-type badRequest string
-
-func (br badRequest) Error() string {
-	return string(br)
-}
-func (br badRequest) BadRequest() {}
-
 type notFound string
 
 func (nf notFound) Error() string {

+ 2 - 2
libnetwork/types/types_test.go

@@ -8,11 +8,11 @@ import (
 func TestErrorConstructors(t *testing.T) {
 	var err error
 
-	err = BadRequestErrorf("Io ho %d uccello", 1)
+	err = InvalidParameterErrorf("Io ho %d uccello", 1)
 	if err.Error() != "Io ho 1 uccello" {
 		t.Fatal(err)
 	}
-	if _, ok := err.(BadRequestError); !ok {
+	if _, ok := err.(InvalidParameterError); !ok {
 		t.Fatal(err)
 	}
 	if _, ok := err.(MaskableError); ok {