Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add UnmarshalErrors() #175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ func MarshalErrors(w io.Writer, errorObjects []*ErrorObject) error {
return json.NewEncoder(w).Encode(&ErrorsPayload{Errors: errorObjects})
}

// UnmarshalErrors is just a wrapper for convenience
func UnmarshalErrors(in io.Reader) (*ErrorsPayload, error) {
payload := new(ErrorsPayload)

if err := json.NewDecoder(in).Decode(payload); err != nil {
return nil, err
}

return payload, nil
}

// ErrorsPayload is a serializer struct for representing a valid JSON API errors payload.
type ErrorsPayload struct {
Errors []*ErrorObject `json:"errors"`
Expand Down
95 changes: 95 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,98 @@ func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
})
}
}

func TestMarshalErrors(t *testing.T) {
firstMeta := map[string]interface{}{
"custom": "info",
"custom two": "more info",
}
secondMeta := map[string]interface{}{
"foo": "foo info",
"bar": "bar info",
}
sample := map[string]interface{}{
"errors": []interface{}{
map[string]interface{}{
"id": "1",
"title": "first title",
"detail": "first detail",
"status": "400",
"code": "first code",
"meta": firstMeta,
},
map[string]interface{}{
"id": "2",
"title": "second title",
"detail": "second detail",
"status": "404",
"code": "second code",
"meta": secondMeta,
},
},
}

data, err := json.Marshal(sample)
if err != nil {
t.Fatal(err)
}
in := bytes.NewReader(data)

errorsPayload, err := UnmarshalErrors(in)
if err != nil {
t.Fatal(err)
}

expectedPayload := ErrorsPayload{
Errors: []*ErrorObject{
{
ID: "1",
Title: "first title",
Detail: "first detail",
Status: "400",
Code: "first code",
Meta: &firstMeta,
}, {
ID: "2",
Title: "second title",
Detail: "second detail",
Status: "404",
Code: "second code",
Meta: &secondMeta,
},
},
}
if !reflect.DeepEqual(*errorsPayload, expectedPayload) {
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", errorsPayload, expectedPayload)
}
}

func TestMarshalErrorsPartialData(t *testing.T) {
sample := map[string]interface{}{
"errors": []interface{}{
map[string]interface{}{
"status": "400",
},
map[string]interface{}{
"status": "404",
},
},
}

data, err := json.Marshal(sample)
if err != nil {
t.Fatal(err)
}
in := bytes.NewReader(data)

errorsPayload, err := UnmarshalErrors(in)
if err != nil {
t.Fatal(err)
}

expectedPayload := ErrorsPayload{Errors: []*ErrorObject{{Status: "400"}, {Status: "404"}}}

if !reflect.DeepEqual(*errorsPayload, expectedPayload) {
t.Fatalf("Expected: \n%#v \nto equal: \n%#v", errorsPayload, expectedPayload)
}
}