controller_resolver_error.go
1020 字节
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
package controllers
import (
_ "embed"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/json"
"strings"
)
//go:embed errors.txt
var errorsData []byte
type CommErrorResolver struct {
// TODO:LRU latest error list
ErrorMapping map[string]string `json:"errorMapping"`
errMapping map[string]error `json:"errorMapping"`
}
func (r *CommErrorResolver) Resolve(err error) error {
if err == nil {
return nil
}
errMsg := err.Error()
if r.errMapping == nil {
return err
}
if v, ok := r.errMapping[errMsg]; ok {
return v
}
for k, v := range r.errMapping {
if strings.Contains(errMsg, k) {
return v
}
}
return err
}
func NewCommErrorResolver() (*CommErrorResolver, error) {
obj := &CommErrorResolver{errMapping: make(map[string]error)}
err := json.Unmarshal(errorsData, obj)
if err != nil {
return nil, err
}
for k, v := range obj.ErrorMapping {
obj.errMapping[k] = application.ThrowError(application.INTERNAL_SERVER_ERROR, v)
}
return obj, nil
}