mirror of
https://github.com/clearlinux/libnetwork.git
synced 2026-12-06 04:46:17 +00:00
Provide interface to categorize errors
- Package types to define the interfaces libnetwork errors may implement, so that caller can categorize them. Signed-off-by: Alessandro Boch <aboch@docker.com>
This commit is contained in:
+23
-5
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/docker/libnetwork"
|
||||
"github.com/docker/libnetwork/types"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
@@ -434,7 +435,7 @@ func findNetwork(c libnetwork.NetworkController, s string, by int) (libnetwork.N
|
||||
panic(fmt.Sprintf("unexpected selector for network search: %d", by))
|
||||
}
|
||||
if err != nil {
|
||||
if err == libnetwork.ErrNoSuchNetwork {
|
||||
if _, ok := err.(libnetwork.ErrNoSuchNetwork); ok {
|
||||
return nil, &responseStatus{Status: "Resource not found: Network", StatusCode: http.StatusNotFound}
|
||||
}
|
||||
return nil, &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest}
|
||||
@@ -460,7 +461,7 @@ func findEndpoint(c libnetwork.NetworkController, ns, es string, nwBy, epBy int)
|
||||
panic(fmt.Sprintf("unexpected selector for endpoint search: %d", epBy))
|
||||
}
|
||||
if err != nil {
|
||||
if err == libnetwork.ErrNoSuchEndpoint {
|
||||
if _, ok := err.(libnetwork.ErrNoSuchEndpoint); ok {
|
||||
return nil, &responseStatus{Status: "Resource not found: Endpoint", StatusCode: http.StatusNotFound}
|
||||
}
|
||||
return nil, &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest}
|
||||
@@ -469,9 +470,26 @@ func findEndpoint(c libnetwork.NetworkController, ns, es string, nwBy, epBy int)
|
||||
}
|
||||
|
||||
func convertNetworkError(err error) *responseStatus {
|
||||
// No real libnetwork error => http error code conversion for now.
|
||||
// Will came in later when new interface for libnetwork error is vailable
|
||||
return &responseStatus{Status: err.Error(), StatusCode: http.StatusBadRequest}
|
||||
var code int
|
||||
switch err.(type) {
|
||||
case types.BadRequestError:
|
||||
code = http.StatusBadRequest
|
||||
case types.ForbiddenError:
|
||||
code = http.StatusForbidden
|
||||
case types.NotFoundError:
|
||||
code = http.StatusNotFound
|
||||
case types.TimeoutError:
|
||||
code = http.StatusRequestTimeout
|
||||
case types.NotImplementedError:
|
||||
code = http.StatusNotImplemented
|
||||
case types.NoServiceError:
|
||||
code = http.StatusServiceUnavailable
|
||||
case types.InternalError:
|
||||
code = http.StatusInternalServerError
|
||||
default:
|
||||
code = http.StatusInternalServerError
|
||||
}
|
||||
return &responseStatus{Status: err.Error(), StatusCode: code}
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, code int, v interface{}) error {
|
||||
|
||||
+91
-2
@@ -193,8 +193,8 @@ func TestCreateDeleteNetwork(t *testing.T) {
|
||||
if errRsp == &createdResponse {
|
||||
t.Fatalf("Expected to fail but succeeded")
|
||||
}
|
||||
if errRsp.StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf("Expected StatusBadRequest status code, got: %v", errRsp.StatusCode)
|
||||
if errRsp.StatusCode != http.StatusNotFound {
|
||||
t.Fatalf("Expected StatusNotFound status code, got: %v", errRsp)
|
||||
}
|
||||
|
||||
ops := make(map[string]interface{})
|
||||
@@ -1305,3 +1305,92 @@ func TestHttpHandlerGood(t *testing.T) {
|
||||
t.Fatalf("Incongruent resource found")
|
||||
}
|
||||
}
|
||||
|
||||
type bre struct{}
|
||||
|
||||
func (b *bre) Error() string {
|
||||
return "I am a bad request error"
|
||||
}
|
||||
func (b *bre) BadRequest() {}
|
||||
|
||||
type nfe struct{}
|
||||
|
||||
func (n *nfe) Error() string {
|
||||
return "I am a not found error"
|
||||
}
|
||||
func (n *nfe) NotFound() {}
|
||||
|
||||
type forb struct{}
|
||||
|
||||
func (f *forb) Error() string {
|
||||
return "I am a bad request error"
|
||||
}
|
||||
func (f *forb) Forbidden() {}
|
||||
|
||||
type notimpl struct{}
|
||||
|
||||
func (nip *notimpl) Error() string {
|
||||
return "I am a not implemented error"
|
||||
}
|
||||
func (nip *notimpl) NotImplemented() {}
|
||||
|
||||
type inter struct{}
|
||||
|
||||
func (it *inter) Error() string {
|
||||
return "I am a internal error"
|
||||
}
|
||||
func (it *inter) Internal() {}
|
||||
|
||||
type tout struct{}
|
||||
|
||||
func (to *tout) Error() string {
|
||||
return "I am a timeout error"
|
||||
}
|
||||
func (to *tout) Timeout() {}
|
||||
|
||||
type noserv struct{}
|
||||
|
||||
func (nos *noserv) Error() string {
|
||||
return "I am a no service error"
|
||||
}
|
||||
func (nos *noserv) NoService() {}
|
||||
|
||||
type notclassified struct{}
|
||||
|
||||
func (noc *notclassified) Error() string {
|
||||
return "I am a non classified error"
|
||||
}
|
||||
|
||||
func TestErrorConversion(t *testing.T) {
|
||||
if convertNetworkError(new(bre)).StatusCode != http.StatusBadRequest {
|
||||
t.Fatalf("Failed to recognize BadRequest error")
|
||||
}
|
||||
|
||||
if convertNetworkError(new(nfe)).StatusCode != http.StatusNotFound {
|
||||
t.Fatalf("Failed to recognize NotFound error")
|
||||
}
|
||||
|
||||
if convertNetworkError(new(forb)).StatusCode != http.StatusForbidden {
|
||||
t.Fatalf("Failed to recognize Forbidden error")
|
||||
}
|
||||
|
||||
if convertNetworkError(new(notimpl)).StatusCode != http.StatusNotImplemented {
|
||||
t.Fatalf("Failed to recognize NotImplemented error")
|
||||
}
|
||||
|
||||
if convertNetworkError(new(inter)).StatusCode != http.StatusInternalServerError {
|
||||
t.Fatalf("Failed to recognize Internal error")
|
||||
}
|
||||
|
||||
if convertNetworkError(new(tout)).StatusCode != http.StatusRequestTimeout {
|
||||
t.Fatalf("Failed to recognize Timeout error")
|
||||
}
|
||||
|
||||
if convertNetworkError(new(noserv)).StatusCode != http.StatusServiceUnavailable {
|
||||
t.Fatalf("Failed to recognize No Service error")
|
||||
}
|
||||
|
||||
if convertNetworkError(new(notclassified)).StatusCode != http.StatusInternalServerError {
|
||||
t.Fatalf("Failed to recognize not classified error as Internal error")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user