handle watch errors everywhere

This commit is contained in:
Daniel Smith
2014-09-22 17:37:12 -07:00
parent 8fd1fb4337
commit f211e46f20
6 changed files with 47 additions and 14 deletions

View File

@@ -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{

View File

@@ -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)
}
}
}