diff --git a/request.go b/request.go index b60968a..31afb77 100644 --- a/request.go +++ b/request.go @@ -65,7 +65,13 @@ func UnmarshalPayload(in io.Reader, model interface{}) error { } -func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) error { +func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) (err error) { + defer func() { + if r := recover(); r != nil { + err = fmt.Errorf("data is not a jsonapi representation of '%v'", model.Type()) + } + }() + modelValue := model.Elem() modelType := model.Type().Elem() diff --git a/request_test.go b/request_test.go index 4143f01..c4a1d7c 100644 --- a/request_test.go +++ b/request_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "io" "regexp" + "strings" "testing" "time" ) @@ -27,6 +28,17 @@ func TestMalformedTag(t *testing.T) { } } +func TestUnmarshalInvalidJSON(t *testing.T) { + in := strings.NewReader("{}") + out := new(Blog) + + err := UnmarshalPayload(in, out) + + if err == nil { + t.Fatalf("Did not error out the invalid JSON.") + } +} + func TestUnmarshalSetsId(t *testing.T) { in := samplePayloadWithId() out := new(Blog)