Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the Source field in the ErrorObject structure #143

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

// ErrorObjectLinks is an implementation of the JSON API error links payload.
//
// For more information on the JSON API spec's error links objects, see: http://jsonapi.org/format/#error-objects
type ErrorObjectLinks struct {
// About is a link that leads to further details about this particular occurrence of the problem.
About string `json:"about"`
}

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

// 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