Skip to content

Commit

Permalink
Merge pull request #24 from honeycombio/jharley.add-error-source-field
Browse files Browse the repository at this point in the history
add 'source' to error object
  • Loading branch information
brandonc authored Aug 2, 2024
2 parents 49e11fe + 3526b7b commit 2490a94
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 16 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,26 @@ type ErrorObject struct {
// Code is an application-specific error code, expressed as a string value.
Code string `json:"code,omitempty"`

// Source is an object containing references to the primary source of 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 an object containing references to the primary source of the error.
// Only one field should be populated depending on the source of the error.
type ErrorSource struct {
// Pointer is a JSON Pointer (RFC6901) indicating the value in the request document that caused the error.
Pointer string `json:"pointer,omitempty"`

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

// Header is a string indicating the name of a single request header which caused the error.
Header string `json:"header,omitempty"`
}

// Error implements the `Error` interface.
func (e *ErrorObject) Error() string {
return fmt.Sprintf("Error: %s %s\n", e.Title, e.Detail)
Expand Down
4 changes: 2 additions & 2 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func TestMarshalErrorsWritesTheExpectedPayload(t *testing.T) {
}{
{
Title: "TestFieldsAreSerializedAsNeeded",
In: []*ErrorObject{{ID: "0", Title: "Test title.", Detail: "Test detail", Status: "400", Code: "E1100"}},
In: []*ErrorObject{{ID: "0", Title: "Test title.", Detail: "Test detail", Status: "400", Code: "E1100", Source: &ErrorSource{Pointer: "title"}}},
Out: map[string]interface{}{"errors": []interface{}{
map[string]interface{}{"id": "0", "title": "Test title.", "detail": "Test detail", "status": "400", "code": "E1100"},
map[string]interface{}{"id": "0", "title": "Test title.", "detail": "Test detail", "status": "400", "code": "E1100", "source": map[string]interface{}{"pointer": "title"}},
}},
},
{
Expand Down

0 comments on commit 2490a94

Please sign in to comment.