-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.go
78 lines (65 loc) · 1.6 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package ShyGinErrors
import (
"fmt"
"reflect"
"github.com/go-playground/validator/v10"
)
type GinErrors interface {
ListAllErrors(model interface{}, err error) map[string]string
}
type ginErrors struct {
errorMaps map[string]string
}
type ErrorResult struct {
Field string
JsonTag string
Message string
}
func NewShyGinErrors(errors map[string]string) GinErrors {
return &ginErrors{
errorMaps: errors,
}
}
func (ge ginErrors) ListAllErrors(model interface{}, err error) map[string]string {
errors := map[string]string{}
fields := map[string]ErrorResult{}
if _, ok := err.(validator.ValidationErrors); ok {
// resolve all json tags for the struct
types := reflect.TypeOf(model)
values := reflect.ValueOf(model)
for i := 0; i < types.NumField(); i++ {
field := types.Field(i)
value := values.Field(i).Interface()
jsonTag := field.Tag.Get("json")
if jsonTag == "" {
jsonTag = field.Name
}
messageTag := field.Tag.Get("msg")
msg := ge.getErrorMessage(messageTag)
fmt.Printf("%s: %v = %v, tag= %v\n", field.Name, field.Type, value, jsonTag)
fields[field.Name] = ErrorResult{
Field: field.Name,
JsonTag: jsonTag,
Message: msg,
}
}
for _, e := range err.(validator.ValidationErrors) {
if field, ok := fields[e.Field()]; ok {
if field.Message != "" {
errors[field.JsonTag] = field.Message
} else {
errors[field.JsonTag] = e.Error()
}
}
}
}else{
errors["0"] = err.Error()
}
return errors
}
func (ge ginErrors) getErrorMessage(key string) string {
if value, ok := ge.errorMaps[key]; ok {
return value
}
return key
}