1package jsonapi
2
3import (
4	"encoding/json"
5	"fmt"
6	"io"
7)
8
9// MarshalErrors writes a JSON API response using the given `[]error`.
10//
11// For more information on JSON API error payloads, see the spec here:
12// http://jsonapi.org/format/#document-top-level
13// and here: http://jsonapi.org/format/#error-objects.
14func MarshalErrors(w io.Writer, errorObjects []*ErrorObject) error {
15	return json.NewEncoder(w).Encode(&ErrorsPayload{Errors: errorObjects})
16}
17
18// ErrorsPayload is a serializer struct for representing a valid JSON API errors payload.
19type ErrorsPayload struct {
20	Errors []*ErrorObject `json:"errors"`
21}
22
23// ErrorObject is an `Error` implementation as well as an implementation of the JSON API error object.
24//
25// The main idea behind this struct is that you can use it directly in your code as an error type
26// and pass it directly to `MarshalErrors` to get a valid JSON API errors payload.
27// For more information on Golang errors, see: https://golang.org/pkg/errors/
28// For more information on the JSON API spec's error objects, see: http://jsonapi.org/format/#error-objects
29type ErrorObject struct {
30	// ID is a unique identifier for this particular occurrence of a problem.
31	ID string `json:"id,omitempty"`
32
33	// Title is a short, human-readable summary of the problem that SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization.
34	Title string `json:"title,omitempty"`
35
36	// Detail is a human-readable explanation specific to this occurrence of the problem. Like title, this field’s value can be localized.
37	Detail string `json:"detail,omitempty"`
38
39	// Status is the HTTP status code applicable to this problem, expressed as a string value.
40	Status string `json:"status,omitempty"`
41
42	// Code is an application-specific error code, expressed as a string value.
43	Code string `json:"code,omitempty"`
44
45	// Meta is an object containing non-standard meta-information about the error.
46	Meta *map[string]interface{} `json:"meta,omitempty"`
47}
48
49// Error implements the `Error` interface.
50func (e *ErrorObject) Error() string {
51	return fmt.Sprintf("Error: %s %s\n", e.Title, e.Detail)
52}
53