Skip to content

Commit

Permalink
Add the Source field in the ErrorObject structure
Browse files Browse the repository at this point in the history
Closes google#130
  • Loading branch information
denouche authored and timrourke committed Jun 10, 2018
1 parent 90068dd commit 1608b34
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
14 changes: 14 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,24 @@ type ErrorObject struct {
// Links is an implementation of the JSON API error links payload.
Links *ErrorObjectLinks `json:"links,omitempty"`

// Source is used to indicate which part of the request document caused the error.
Source *ErrorSource `json:"source,omitempty"`

// Meta is an object containing non-standard meta-information about the error.
Meta *map[string]interface{} `json:"meta,omitempty"`
}

// ErrorSource is a structure containing references to the source of the error, optionally including any of the following members:
//
// For more information on the JSON API spec's error objects, see: http://jsonapi.org/format/#error-objects
type ErrorSource struct {
// 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].
Pointer string `json:"pointer,omitempty"`

// Parameter is a string indicating which URI query parameter caused the error.
Parameter string `json:"parameter,omitempty"`
}

// Error implements the `Error` interface.
func (e *ErrorObject) Error() string {
return fmt.Sprintf("Error: %s %s\n", e.Title, e.Detail)
Expand Down
7 changes: 7 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
map[string]interface{}{"title": "Test title.", "detail": "Test detail", "meta": map[string]interface{}{"key": "val"}},
}},
},
{
Title: "TestSourceFieldIsSerializedAsNeeded",
In: []*ErrorObject{{Title: "Test title.", Detail: "Test detail", Source: &ErrorSource{Pointer: "/data/attributes/foobar", Parameter: "foobar"}}},
Out: map[string]interface{}{"errors": []interface{}{
map[string]interface{}{"title": "Test title.", "detail": "Test detail", "source": map[string]interface{}{"pointer": "/data/attributes/foobar", "parameter": "foobar"}},
}},
},
}
for _, testRow := range marshalErrorsTableTasts {
t.Run(testRow.Title, func(t *testing.T) {
Expand Down

0 comments on commit 1608b34

Please sign in to comment.