Skip to content

Commit 6a9d00b

Browse files
Merge pull request #2 from denouche/addErrorObjectSource
Add the Source field in the ErrorObject structure
2 parents dc81607 + c563af5 commit 6a9d00b

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

45+
// Source is used to indicate which part of the request document caused the error.
46+
Source *ErrorSource `json:"source,omitempty"`
47+
4548
// Meta is an object containing non-standard meta-information about the error.
4649
Meta *map[string]interface{} `json:"meta,omitempty"`
4750
}
4851

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