…lines 1–8 unchanged (8 lines)
9 "fmt"
10 "reflect"
11
12 "github.com/gin-gonic/gin/internal/json"
13)
14
15// ErrorType is an unsigned 64-bit error code as defined in the gin spec.
16type ErrorType uint64
17
18const (
19 // ErrorTypeBind is used when Context.Bind() fails.
20 ErrorTypeBind ErrorType = 1 << 63
21 // ErrorTypeRender is used when Context.Render() fails.
22 ErrorTypeRender ErrorType = 1 << 62
23 // ErrorTypePrivate indicates a private error.
24 ErrorTypePrivate ErrorType = 1 << 0
25 // ErrorTypePublic indicates a public error.
26 ErrorTypePublic ErrorType = 1 << 1
27 // ErrorTypeAny indicates other any error.
28 ErrorTypeAny ErrorType = 1<<64 - 1
29 ErrorTypeNu = 2
30)
31
32// Error represents a error's specification.
33type Error struct {
34 Err error
35 Type ErrorType
36 Meta interface{}
37}
38
39type errorMsgs []*Error
40
41var _ error = &Error{}
42
43// SetType sets the error's type.
44func (msg *Error) SetType(flags ErrorType) *Error {
45 msg.Type = flags
46 return msg
47}
48
49// SetMeta sets the error's meta data.
50func (msg *Error) SetMeta(data interface{}) *Error {
51 msg.Meta = data
52 return msg
53}
54
55func (msg *Error) JSON() interface{} {
…lines 56–73 unchanged (18 lines)
74}
75
76// MarshalJSON implements the json.Marshaller interface.
77func (msg *Error) MarshalJSON() ([]byte, error) {
78 return json.Marshal(msg.JSON())
79}
80
81// Error implements the error interface.
82func (msg Error) Error() string {
83 return msg.Err.Error()
84}
85
86// IsType judges one error.
87func (msg *Error) IsType(flags ErrorType) bool {
88 return (msg.Type & flags) > 0
89}
90
91// ByType returns a readonly copy filtered the byte.
92// ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic.
…lines 93–143 unchanged (51 lines)
144 json[i] = err.JSON()
145 }
146 return json
147 }
148}
149
150// MarshalJSON implements the json.Marshaller interface.
151func (a errorMsgs) MarshalJSON() ([]byte, error) {
152 return json.Marshal(a.JSON())
153}
154
155func (a errorMsgs) String() string {
156 if len(a) == 0 {
…lines 157–168 unchanged (12 lines)