-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
62 lines (53 loc) · 1.81 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
package jsonapi
// ErrorSource is the standard JSONAPI Error Source struct
type ErrorSource struct {
Pointer string `json:"pointer,omitempty"`
Parameter string `json:"parameter,omitempty"`
}
// Error is the standard JSONAPI Error struct
type Error struct {
ID string `json:"id,omitempty"`
Links Links `json:"links,omitempty"`
Status int `json:"status,omitempty"`
Code int `json:"code,omitempty"`
Title string `json:"title,omitempty"`
Detail string `json:"detail,omitempty"`
Source interface{} `json:"source,omitempty"` // ErrorSource
Meta interface{} `json:"meta,omitempty"`
}
// Errors is a standard array of JSONAPI Error structs
type Errors []Error
// HasErrors checks if Errors array has one more errors.
// data key cannot coexist in response with errors: https://jsonapi.org/format/#document-top-level
func (errs Errors) HasErrors() bool {
return len(errs) > 0
}
type internalError struct {
ID string `json:"id,omitempty"`
Links LinkMap `json:"links,omitempty"`
Status int `json:"status,omitempty"`
Code int `json:"code,omitempty"`
Title string `json:"title,omitempty"`
Detail string `json:"detail,omitempty"`
Source interface{} `json:"source,omitempty"` // ErrorSource
Meta interface{} `json:"meta,omitempty"`
}
func transformErrors(errs []Error, baseURL string) []internalError {
var internalErrors []internalError
for _, err := range errs {
internalErrors = append(internalErrors, transformError(err, baseURL))
}
return internalErrors
}
func transformError(err Error, baseURL string) internalError {
return internalError{
ID: err.ID,
Links: TransformLinks(err.Links, baseURL),
Status: err.Status,
Code: err.Code,
Title: err.Title,
Detail: err.Detail,
Source: err.Source,
Meta: err.Meta,
}
}