Skip to content

Commit c563af5

Browse files
committed
Add the Source field in the ErrorObject structure
Closes google#130
1 parent 2dcc18f commit c563af5

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
@@ -45,10 +45,24 @@ type ErrorObject struct {
4545
// Code is an application-specific error code, expressed as a string value.
4646
Code string `json:"code,omitempty"`
4747

48+
// Source is used to indicate which part of the request document caused the error.
49+
Source *ErrorSource `json:"source,omitempty"`
50+
4851
// Meta is an object containing non-standard meta-information about the error.
4952
Meta *map[string]interface{} `json:"meta,omitempty"`
5053
}
5154

55+
// ErrorSource is a structure containing references to the source of the error, optionally including any of the following members:
56+
//
57+
// For more information on the JSON API spec's error objects, see: http://jsonapi.org/format/#error-objects
58+
type ErrorSource struct {
59+
// 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].
60+
Pointer string `json:"pointer,omitempty"`
61+
62+
// Parameter is a string indicating which URI query parameter caused the error.
63+
Parameter string `json:"parameter,omitempty"`
64+
}
65+
5266
// Error implements the `Error` interface.
5367
func (e *ErrorObject) Error() string {
5468
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)