forked from google/jsonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
99 lines (83 loc) · 3.36 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package jsonapi
import (
"encoding/json"
"fmt"
"io"
"strings"
)
// MarshalErrors writes a JSON API response using the given `[]error`.
//
// For more information on JSON API error payloads, see the spec here:
// http://jsonapi.org/format/#document-top-level
// and here: http://jsonapi.org/format/#error-objects.
func MarshalErrors(w io.Writer, errorObjects []*ErrorObject) error {
return json.NewEncoder(w).Encode(&ErrorsPayload{Errors: errorObjects})
}
// ErrorsPayload is a serializer struct for representing a valid JSON API errors payload.
type ErrorsPayload struct {
Errors []*ErrorObject `json:"errors"`
}
// Code represents the application-specific error code, expressed as a string value.
type Code string
// UnmarshalJSON converts the JSON number or string value into Code string
func (c *Code) UnmarshalJSON(data []byte) error {
var rawV interface{}
if err := json.Unmarshal(data, &rawV); err != nil {
return err
}
switch v := rawV.(type) {
case string:
*c = Code(v)
case float64:
*c = Code(fmt.Sprintf("%.0f", v))
default:
return fmt.Errorf("json: cannot unmarshal into Go value of type %T ", v)
}
return nil
}
// 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
// and pass it directly to `MarshalErrors` to get a valid JSON API errors payload.
// For more information on Golang errors, see: https://golang.org/pkg/errors/
// For more information on the JSON API spec's error objects, see: http://jsonapi.org/format/#error-objects
type ErrorObject struct {
// ID is a unique identifier for this particular occurrence of a problem.
ID string `json:"id,omitempty"`
// 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.
Title string `json:"title,omitempty"`
// Detail is a human-readable explanation specific to this occurrence of the problem. Like title, this field’s value can be localized.
Detail string `json:"detail,omitempty"`
// Status is the HTTP status code applicable to this problem, expressed as a string value.
Status string `json:"status,omitempty"`
// Code is an application-specific error code, expressed as a string value.
Code Code `json:"code,omitempty"`
// Source is the source of the error, this is used for validation 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"`
}
// Source represents an Error source such as
//
// "source": {
// "pointer": "/data/attributes/linkedInUrl"
// }
type errorSource struct {
// Pointer is the actual location of the error source e.g: "pointer": "/data/attributes/linkedInUrl"
Pointer string `json:"pointer,omitempty"`
}
// Error implements the `Error` interface.
func (e *ErrorObject) Error() string {
return fmt.Sprintf("Error: %s %s\n", e.Title, e.Detail)
}
// MultiError allows multiple JSON:API compliant errors to be returned as a singular
// stdlib error.
type MultiError []*ErrorObject
// Error implements the error interface for MultiError.
func (m MultiError) Error() string {
errs := make([]string, 0, len(m))
for i := range m {
errs = append(errs, m[i].Error())
}
return strings.Join(errs, ", ")
}