-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.go
More file actions
87 lines (71 loc) · 1.83 KB
/
errors.go
File metadata and controls
87 lines (71 loc) · 1.83 KB
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
package gencheck
import (
"bytes"
"fmt"
"strings"
)
const (
errMsgFormat = `validation: field validation failed for '%s.%s': %s`
errTagFormat = `validation: field validation failed for '%s.%s': tag='%s'`
)
// Error returns a string of the validation errors
func (ve ValidationErrors) Error() string {
buff := bytes.NewBufferString("")
var fe *fieldError
for i := 0; i < len(ve); i++ {
fe = ve[i].(*fieldError)
buff.WriteString(fe.Error())
buff.WriteString("\n")
}
return strings.TrimSpace(buff.String())
}
// ValidationErrors is an array of Field Errors
type ValidationErrors []FieldError
// FieldError provide error information for a specific field validation.
type FieldError interface {
Tag() string
Field() string
Struct() string
Message() string
error
}
// fieldError provide error information for a specific field validation.
type fieldError struct {
Ftag string `json:"tag"`
Ffield string `json:"field"`
FstructName string `json:"structName"`
Fmsg string `json:"msg,omitempty"`
}
func (fe fieldError) Error() string {
if fe.Fmsg == "" {
return fmt.Sprintf(errTagFormat, fe.FstructName, fe.Ffield, fe.Ftag)
}
return fmt.Sprintf(errMsgFormat, fe.FstructName, fe.Ffield, fe.Fmsg)
}
func (fe fieldError) Tag() string {
return fe.Ftag
}
func (fe fieldError) Field() string {
return fe.Ffield
}
func (fe fieldError) Struct() string {
return fe.FstructName
}
func (fe fieldError) Message() string {
return fe.Fmsg
}
// NewFieldError returns a newly created immutable FieldError
// Tag is normally the rule that was invalid, but can be used for whatever
// message you want.
func NewFieldError(st, field, tag string, err error) FieldError {
var msg string
if err != nil {
msg = err.Error()
}
return &fieldError{
FstructName: st,
Ffield: field,
Ftag: tag,
Fmsg: msg,
}
}