-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
145 lines (116 loc) · 2.85 KB
/
error.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
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
package goval
import (
"encoding/json"
"fmt"
"strings"
)
type Errors struct {
Errors []Error
}
func (e Errors) String() string {
return e.Error()
}
func (e Errors) Error() string {
var els []string
for _, er := range e.Errors {
els = append(els, er.String())
}
return "[" + strings.Join(els, ",") + "]"
}
func (e Errors) StringWithMessage() string {
var els []string
for _, er := range e.Errors {
els = append(els, er.StringWithMessage())
}
return "[" + strings.Join(els, ",") + "]"
}
func (e Errors) StringWithMessageLang(ln string) string {
var els []string
for _, er := range e.Errors {
els = append(els, er.StringWithMessageLang(ln))
}
return "[" + strings.Join(els, ",") + "]"
}
//// NewCustomError creates a new custom error
//func NewCustomError(messageID int) Error {
// return Error{
// Type: ErrorTypeCustom,
// Values: make(map[string]any),
// Path: []string{},
// customMessageID: messageID,
// }
//}
//// NewError creates a new error
//func NewError(t ErrorType) Error {
// return Error{
// Type: t,
// Values: make(map[string]any),
// Path: []string{},
// }
//}
//// AddValue adds a value to the error
//func (e Error) AddValue(key string, value any) Error {
// e.Values[key] = value
// return e
//}
// Error is a struct that represents an error
type Error struct {
Type ErrorType `json:"type,omitempty"`
Field string `json:"field,omitempty"`
Message string `json:"message,omitempty"`
Values map[string]any `json:"values,omitempty"`
Path []string `json:"path,omitempty"`
customMessageID int
}
func (e Error) customMessageStringLang(ln string) string {
lang, ok := customMessages[e.customMessageID]
if !ok {
return "Unknown custom message"
}
s, ok := lang[ln]
if !ok {
return "Undefined lang for custom message"
}
for k, v := range e.Values {
s = strings.Replace(s, "{"+k+"}", fmt.Sprintf("%v", v), -1)
}
return s
}
// StringWithMessageLang returns the error message in the specified language
func (e Error) StringWithMessageLang(ln string) string {
if e.customMessageID > 0 {
e.Message = e.customMessageStringLang(ln)
res, err := json.Marshal(e)
if err != nil {
return `{"error":"error while marshalling error"}`
}
return string(res)
}
lang, ok := langs[ln]
if !ok {
lang = langEn
}
s, ok := lang[e.Type]
if !ok {
return `{"error":"unknown error"}`
}
for k, v := range e.Values {
s = strings.Replace(s, "{"+k+"}", fmt.Sprintf("%v", v), -1)
}
e.Message = s
res, err := json.Marshal(e)
if err != nil {
return `{"error":"error while marshalling error"}`
}
return string(res)
}
func (e Error) String() string {
res, err := json.Marshal(e)
if err != nil {
return `{"error":"error while marshalling error"}`
}
return string(res)
}
func (e Error) StringWithMessage() string {
return e.StringWithMessageLang(defaultLang)
}