Skip to content
Closed
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
13 changes: 13 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ type ErrorsPayload struct {
Errors []*ErrorObject `json:"errors"`
}

// ErrorObject implements the JSON API `source` object (http://jsonapi.org/format/#error-objects).
type ErrorSource struct {
// If the error comes from validating data from a client,
// this is the `path` of the attribute that has generated the error.
// es. `/data/attributes/email`.
Pointer string `json:"pointer,omitempty"`
// A string indicating which URI query parameter caused the error.
Parameter string `json:"parameter,omitempty"`
}

// ErrorObject is an `Error` implementation as well as an implementation of the JSON API error object.
//
// The main idea behind this struct is that you can use it directly in your code as an error type
Expand All @@ -47,6 +57,9 @@ type ErrorObject struct {

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

// Source contains reference to the source of the error.
Source *ErrorSource `json:"source,omitempty"`
}

// Error implements the `Error` interface.
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: "TestSourceFieldIsSerializedProperly",
In: []*ErrorObject{{Source: &ErrorSource{Pointer: "/data/attributes/email", Parameter: "email"}}},
Out: map[string]interface{}{"errors": []interface{}{
map[string]interface{}{"source": map[string]interface{}{"pointer": "/data/attributes/email", "parameter": "email"}},
}},
},
}
for _, testRow := range marshalErrorsTableTasts {
t.Run(testRow.Title, func(t *testing.T) {
Expand Down