Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patherrors.go
More file actions
Latest commit
129 lines (101 loc) · 2.72 KB
/
Copy patherrors.go
File metadata and controls
129 lines (101 loc) · 2.72 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Errors package implements the JSON API v1.0 format for errors.
// Ref: http://jsonapi.org/format/#errors
package errors
import"fmt"
// Source contain references to the source of the error
typeSourcestruct {
Pointerstring`json:"pointer,omitempty"`
Parameterstring`json:"parameter,omitempty"`
}
// Link should lead to further details about this error
typeLinkstruct {
Aboutstring`json:"about,omitempty"`
}
// Error provides additional information about problems encountered while
// performing an operation.
typeErrorstruct {
Codeint`json:"code,string,omitempty"`
Debugmap[string]interface{} `json:"debug,omitempty"`
Detailstring`json:"detail"`
IDstring`json:"id,omitempty"`
Links*Link`json:"links,omitempty"`
Metamap[string]interface{} `json:"meta,omitempty"`
Source*Source`json:"source,omitempty"`
Statusint`json:"status,string"`
Titlestring`json:"title,omitempty"`
}
func (e*Error) Error() string {
returnfmt.Sprintf("%d: %s", e.Status, e.Detail)
}
func (e*Error) SetAboutLink(linkstring) {
l:=&Link{link}
e.Links=l
}
func (e*Error) AddSourceNode() *Source {
s:=&Source{}
e.Source=s
returns
}
func (e*Error) SetPointer(pointerstring) *Error {
ife.Source==nil {
e.AddSourceNode()
}
e.Source.Pointer=pointer
returne
}
func (e*Error) SetParameter(parameterstring) *Error {
ife.Source==nil {
e.AddSourceNode()
}
e.Source.Parameter=parameter
returne
}
funcNewError(statusint, detailstring) *Error {
return&Error{
Status: status,
Detail: detail,
}
}
typeBagstruct {
Errors []*Error`json:"errors"`
Statusint`json:"status,string,omitempty"`
}
func (b*Bag) Add(err*Error) *Error {
b.Errors=append(b.Errors, err)
iflen(b.Errors) ==1 {
b.Status=err.Status
returnerr
}
ifb.Status!=err.Status {
ifstatusClass:=GetErrorClass(b.Status); statusClass==GetErrorClass(err.Status) {
b.Status=statusClass
} else {
b.Status=400
}
}
returnerr
}
func (b*Bag) AddError(statusint, detailstring) *Error {
returnb.Add(NewError(status, detail))
}
// NewBag returns a new Bag
funcNewBag() *Bag {
return&Bag{}
}
// NewBagWithError is a shorthand to `errors.NewBag().Add(status, detail)`
funcNewBagWithError(statusint, detailstring) *Bag {
bag:=&Bag{}
bag.AddError(status, detail)
returnbag
}
// GetErrorClass returns 400 or 500 depending of the error code passed.
// If the error code is less than 400 or greater or equal to 600 it returns 0.
funcGetErrorClass(errint) int {
iferr<400||err>=600 {
return0
}
iferr>=400&&err<500 {
return400
}
return500
}