-
Notifications
You must be signed in to change notification settings - Fork 1
/
err.go
57 lines (46 loc) · 826 Bytes
/
err.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
package ctx
import (
"fmt"
"reflect"
"strconv"
)
type ErrorCode int
type Error struct {
Code ErrorCode
Errors Errors
}
func ErrorStatusCode(code int) int {
i, _ := strconv.Atoi(fmt.Sprintf("%d", code)[:3])
return i
}
func NewErrors(s ...interface{}) *Errors {
r := &Errors{
[]interface{}{},
}
for _, v := range s {
rt := reflect.TypeOf(v)
switch rt.Kind() {
case reflect.Slice, reflect.Array:
v1 := reflect.ValueOf(v)
for i := 0; i < v1.Len(); i++ {
r.s = append(r.s, v1.Index(i).Interface())
}
default:
r.s = append(r.s, v)
}
}
return r
}
type Errors struct {
s []interface{}
}
func (d *Errors) Add(s interface{}) *Errors {
d.s = append(d.s, s)
return d
}
func (d *Errors) Error() []interface{} {
return d.s
}
func (d *Errors) NotEmpty() bool {
return len(d.s) > 0
}