forked from gin-gonic/gin
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
Latest commit
159 lines (139 loc) · 3.16 KB
/
Copy patherrors.go
File metadata and controls
159 lines (139 loc) · 3.16 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package gin
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
)
typeErrorTypeuint64
const (
ErrorTypeBindErrorType=1<<63// used when c.Bind() fails
ErrorTypeRenderErrorType=1<<62// used when c.Render() fails
ErrorTypePrivateErrorType=1<<0
ErrorTypePublicErrorType=1<<1
ErrorTypeAnyErrorType=1<<64-1
ErrorTypeNu=2
)
type (
Errorstruct {
Errerror
TypeErrorType
Metainterface{}
}
errorMsgs []*Error
)
var_error=&Error{}
func (msg*Error) SetType(flagsErrorType) *Error {
msg.Type=flags
returnmsg
}
func (msg*Error) SetMeta(datainterface{}) *Error {
msg.Meta=data
returnmsg
}
func (msg*Error) JSON() interface{} {
json:=H{}
ifmsg.Meta!=nil {
value:=reflect.ValueOf(msg.Meta)
switchvalue.Kind() {
casereflect.Struct:
returnmsg.Meta
casereflect.Map:
for_, key:=rangevalue.MapKeys() {
json[key.String()] =value.MapIndex(key).Interface()
}
default:
json["meta"] =msg.Meta
}
}
if_, ok:=json["error"]; !ok {
json["error"] =msg.Error()
}
returnjson
}
// MarshalJSON implements the json.Marshaller interface
func (msg*Error) MarshalJSON() ([]byte, error) {
returnjson.Marshal(msg.JSON())
}
// Implements the error interface
func (msg*Error) Error() string {
returnmsg.Err.Error()
}
func (msg*Error) IsType(flagsErrorType) bool {
return (msg.Type&flags) >0
}
// Returns a readonly copy filterd the byte.
// ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic
func (aerrorMsgs) ByType(typErrorType) errorMsgs {
iflen(a) ==0 {
returnnil
}
iftyp==ErrorTypeAny {
returna
}
varresulterrorMsgs
for_, msg:=rangea {
ifmsg.IsType(typ) {
result=append(result, msg)
}
}
returnresult
}
// Returns the last error in the slice. It returns nil if the array is empty.
// Shortcut for errors[len(errors)-1]
func (aerrorMsgs) Last() *Error {
length:=len(a)
iflength>0 {
returna[length-1]
}
returnnil
}
// Returns an array will all the error messages.
// Example:
// c.Error(errors.New("first"))
// c.Error(errors.New("second"))
// c.Error(errors.New("third"))
// c.Errors.Errors() // == []string{"first", "second", "third"}
func (aerrorMsgs) Errors() []string {
iflen(a) ==0 {
returnnil
}
errorStrings:=make([]string, len(a))
fori, err:=rangea {
errorStrings[i] =err.Error()
}
returnerrorStrings
}
func (aerrorMsgs) JSON() interface{} {
switchlen(a) {
case0:
returnnil
case1:
returna.Last().JSON()
default:
json:=make([]interface{}, len(a))
fori, err:=rangea {
json[i] =err.JSON()
}
returnjson
}
}
func (aerrorMsgs) MarshalJSON() ([]byte, error) {
returnjson.Marshal(a.JSON())
}
func (aerrorMsgs) String() string {
iflen(a) ==0 {
return""
}
varbuffer bytes.Buffer
fori, msg:=rangea {
fmt.Fprintf(&buffer, "Error #%02d: %s\n", (i+1), msg.Err)
ifmsg.Meta!=nil {
fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)
}
}
returnbuffer.String()
}