Browse code

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>

Sebastiaan van Stijn authored on 2016/07/25 19:15:54
Showing 1 changed files
... ...
@@ -8,6 +8,7 @@ import (
8 8
 	"golang.org/x/net/context"
9 9
 
10 10
 	"github.com/docker/docker/api/server/httputils"
11
+	"github.com/docker/docker/errors"
11 12
 	"github.com/docker/engine-api/types"
12 13
 	"github.com/docker/engine-api/types/filters"
13 14
 	"github.com/docker/engine-api/types/network"
... ...
@@ -121,7 +122,8 @@ func (n *networkRouter) postNetworkConnect(ctx context.Context, w http.ResponseW
121 121
 	}
122 122
 
123 123
 	if nw.Info().Dynamic() {
124
-		return newNetworkForbiddenError("operation not supported for swarm scoped networks")
124
+		err := fmt.Errorf("operation not supported for swarm scoped networks")
125
+		return errors.NewRequestForbiddenError(err)
125 126
 	}
126 127
 
127 128
 	return n.backend.ConnectContainerToNetwork(connect.Container, nw.Name(), connect.EndpointConfig)
... ...
@@ -147,7 +149,8 @@ func (n *networkRouter) postNetworkDisconnect(ctx context.Context, w http.Respon
147 147
 	}
148 148
 
149 149
 	if nw.Info().Dynamic() {
150
-		return newNetworkForbiddenError("operation not supported for swarm scoped networks")
150
+		err := fmt.Errorf("operation not supported for swarm scoped networks")
151
+		return errors.NewRequestForbiddenError(err)
151 152
 	}
152 153
 
153 154
 	return n.backend.DisconnectContainerFromNetwork(disconnect.Container, nw, disconnect.Force)
... ...
@@ -292,17 +295,3 @@ func buildEndpointResource(e libnetwork.Endpoint) types.EndpointResource {
292 292
 	}
293 293
 	return er
294 294
 }
295
-
296
-// networkForbiddenError represents an authorization deny error
297
-type networkForbiddenError struct {
298
-	error
299
-}
300
-
301
-// HTTPErrorStatusCode returns the authorization error status code (forbidden)
302
-func (e networkForbiddenError) HTTPErrorStatusCode() int {
303
-	return http.StatusForbidden
304
-}
305
-
306
-func newNetworkForbiddenError(msg string) networkForbiddenError {
307
-	return networkForbiddenError{error: fmt.Errorf("%s", msg)}
308
-}