Skip to content

Commit 1608b34

Browse files
denouchetimrourke
authored andcommitted
Add the Source field in the ErrorObject structure
Closes google#130
1 parent 90068dd commit 1608b34

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

errors.go

+14
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,24 @@ type ErrorObject struct {
5656
// Links is an implementation of the JSON API error links payload.
5757
Links *ErrorObjectLinks `json:"links,omitempty"`
5858

59+
// Source is used to indicate which part of the request document caused the error.
60+
Source *ErrorSource `json:"source,omitempty"`
61+
5962
// Meta is an object containing non-standard meta-information about the error.
6063
Meta *map[string]interface{} `json:"meta,omitempty"`
6164
}
6265

66+
// ErrorSource is a structure containing references to the source of the error, optionally including any of the following members:
67+
//
68+
// For more information on the JSON API spec's error objects, see: http://jsonapi.org/format/#error-objects
69+
type ErrorSource struct {
70+
// Pointer is a JSON Pointer [RFC6901] to the associated entity in the request document [e.g. "/data" for a primary data object, or "/data/attributes/title" for a specific attribute].
71+
Pointer string `json:"pointer,omitempty"`
72+
73+
// Parameter is a string indicating which URI query parameter caused the error.
74+
Parameter string `json:"parameter,omitempty"`
75+
}
76+
6377
// Error implements the `Error` interface.
6478
func (e *ErrorObject) Error() string {
6579
return fmt.Sprintf("Error: %s %s\n", e.Title, e.Detail)

errors_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
4040
map[string]interface{}{"title": "Test title.", "detail": "Test detail", "meta": map[string]interface{}{"key": "val"}},
4141
}},
4242
},
43+
{
44+
Title: "TestSourceFieldIsSerializedAsNeeded",
45+
In: []*ErrorObject{{Title: "Test title.", Detail: "Test detail", Source: &ErrorSource{Pointer: "/data/attributes/foobar", Parameter: "foobar"}}},
46+
Out: map[string]interface{}{"errors": []interface{}{
47+
map[string]interface{}{"title": "Test title.", "detail": "Test detail", "source": map[string]interface{}{"pointer": "/data/attributes/foobar", "parameter": "foobar"}},
48+
}},
49+
},
4350
}
4451
for _, testRow := range marshalErrorsTableTasts {
4552
t.Run(testRow.Title, func(t *testing.T) {

0 commit comments

Comments
 (0)