From 1608b34bde2f0f22ec7c4f7afb2e5b4f2b0df5e6 Mon Sep 17 00:00:00 2001 From: "antoine.leveugle" Date: Fri, 16 Mar 2018 15:54:07 +0100 Subject: [PATCH] Add the Source field in the ErrorObject structure Closes #130 --- errors.go | 14 ++++++++++++++ errors_test.go | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/errors.go b/errors.go index 9696e86..ee9f10e 100644 --- a/errors.go +++ b/errors.go @@ -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) diff --git a/errors_test.go b/errors_test.go index 683a1d1..3b9e1fc 100644 --- a/errors_test.go +++ b/errors_test.go @@ -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) {