Browse Source

make network errors less DRY

There's existing code to generate these
kind of errors, so make the errors added
in commit cc493a52a46271df82dbebea26038502b85788b9
less DRY.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 3fa9d77bf312652ae04e902a2b6e73a0b91ec007)
Signed-off-by: Tibor Vass <tibor@docker.com>
Sebastiaan van Stijn 9 years ago
parent
commit
ec241bfeaf
1 changed files with 5 additions and 16 deletions
  1. 5 16
      api/server/router/network/network_routes.go

+ 5 - 16
api/server/router/network/network_routes.go

@@ -8,6 +8,7 @@ import (
 	"golang.org/x/net/context"
 
 	"github.com/docker/docker/api/server/httputils"
+	"github.com/docker/docker/errors"
 	"github.com/docker/engine-api/types"
 	"github.com/docker/engine-api/types/filters"
 	"github.com/docker/engine-api/types/network"
@@ -121,7 +122,8 @@ func (n *networkRouter) postNetworkConnect(ctx context.Context, w http.ResponseW
 	}
 
 	if nw.Info().Dynamic() {
-		return newNetworkForbiddenError("operation not supported for swarm scoped networks")
+		err := fmt.Errorf("operation not supported for swarm scoped networks")
+		return errors.NewRequestForbiddenError(err)
 	}
 
 	return n.backend.ConnectContainerToNetwork(connect.Container, nw.Name(), connect.EndpointConfig)
@@ -147,7 +149,8 @@ func (n *networkRouter) postNetworkDisconnect(ctx context.Context, w http.Respon
 	}
 
 	if nw.Info().Dynamic() {
-		return newNetworkForbiddenError("operation not supported for swarm scoped networks")
+		err := fmt.Errorf("operation not supported for swarm scoped networks")
+		return errors.NewRequestForbiddenError(err)
 	}
 
 	return n.backend.DisconnectContainerFromNetwork(disconnect.Container, nw, disconnect.Force)
@@ -292,17 +295,3 @@ func buildEndpointResource(e libnetwork.Endpoint) types.EndpointResource {
 	}
 	return er
 }
-
-// networkForbiddenError represents an authorization deny error
-type networkForbiddenError struct {
-	error
-}
-
-// HTTPErrorStatusCode returns the authorization error status code (forbidden)
-func (e networkForbiddenError) HTTPErrorStatusCode() int {
-	return http.StatusForbidden
-}
-
-func newNetworkForbiddenError(msg string) networkForbiddenError {
-	return networkForbiddenError{error: fmt.Errorf("%s", msg)}
-}