-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
49 lines (42 loc) · 1.14 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
package v
import (
"fmt"
"strings"
)
// ErrorRequired is the type of error thrown when a required field is missing
type ErrorRequired struct {
Field string
JSONName string
}
// Error satisfies the builtin Error interface
func (e ErrorRequired) Error() string {
if e.JSONName != "" {
return fmt.Sprintf("[validation] %s: required, please provide a value", e.JSONName)
}
return fmt.Sprintf("[validation] %s: required, please provide a value", e.Field)
}
// ErrorValidation is the type of error thrown on a validation error
type ErrorValidation struct {
Name string
JSONName string
Err error
}
// Error satisfies the builtin Error interface
func (e ErrorValidation) Error() string {
if e.JSONName != "" {
return fmt.Sprintf("[validation] %s: %v", e.JSONName, e.Err)
}
return fmt.Sprintf("[validation] %s: %v", e.Name, e.Err)
}
type validationErorrs []error
// Error satisfies the builtin Error interface
func (v validationErorrs) Error() error {
if len(v) == 0 {
return nil
}
var messages []string
for _, err := range v {
messages = append(messages, err.Error())
}
return fmt.Errorf("%s", strings.Join(messages, " | "))
}