-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
55 lines (46 loc) · 1014 Bytes
/
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
package backend
import "strings"
type (
// InputError collection.
InputErrors []InputError
// InputError contains field name and error type.
InputError struct {
FullName string
Name string
Kind string
Type string
Param string
}
// InputErrorWithIndex contains InputError and struct index in a slice.
InputErrorWithIndex struct {
InputError
Index int
}
)
func (err InputError) Error() string {
return err.Name + ": " + err.Type
}
func (errs InputErrors) Error() string {
var msgs []string
for _, err := range errs {
msgs = append(msgs, err.Error())
}
return "Errors: " + strings.Join(msgs, ", ")
}
func (errs InputErrors) PanicIfPresent() {
if len(errs) > 0 {
panic(errs)
}
}
func NewInputErrors(name, errType string) InputErrors {
return InputErrors{NewInputError(name, errType)}
}
func NewInputError(name, errType string) InputError {
return InputError{
FullName: name,
Name: name,
Kind: "string",
Type: errType,
Param: "",
}
}