handle watch errors everywhere
This commit is contained in:
@@ -21,6 +21,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||
)
|
||||
|
||||
// statusError is an error intended for consumption by a REST API server.
|
||||
@@ -38,6 +39,16 @@ func (e *statusError) Status() api.Status {
|
||||
return e.status
|
||||
}
|
||||
|
||||
// FromObject generates an statusError from an api.Status, if that is the type of obj; otherwise,
|
||||
// returns an error created by fmt.Errorf.
|
||||
func FromObject(obj runtime.Object) error {
|
||||
switch t := obj.(type) {
|
||||
case *api.Status:
|
||||
return &statusError{*t}
|
||||
}
|
||||
return fmt.Errorf("unexpected object: %v", obj)
|
||||
}
|
||||
|
||||
// NewNotFound returns a new error which indicates that the resource of the kind and the name was not found.
|
||||
func NewNotFound(kind, name string) error {
|
||||
return &statusError{api.Status{
|
||||
|
@@ -23,6 +23,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||
)
|
||||
|
||||
func TestErrorNew(t *testing.T) {
|
||||
@@ -131,3 +132,23 @@ func Test_reasonForError(t *testing.T) {
|
||||
t.Errorf("unexpected reason type: %#v", a)
|
||||
}
|
||||
}
|
||||
|
||||
type TestType struct{}
|
||||
|
||||
func (*TestType) IsAnAPIObject() {}
|
||||
|
||||
func TestFromObject(t *testing.T) {
|
||||
table := []struct {
|
||||
obj runtime.Object
|
||||
message string
|
||||
}{
|
||||
{&api.Status{Message: "foobar"}, "foobar"},
|
||||
{&TestType{}, "unexpected object: &{}"},
|
||||
}
|
||||
|
||||
for _, item := range table {
|
||||
if e, a := item.message, FromObject(item.obj).Error(); e != a {
|
||||
t.Errorf("Expected %v, got %v", e, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user