This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
errors.go
133 lines (114 loc) · 3.48 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
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
package api
import (
"fmt"
)
// ValidationResult is an interface to be used by errors that return a properly formatted struct
// to be used to send for client-side validation requests.
type ValidationResult interface {
Result(fieldname string) interface{}
}
// ErrorStackResults is an interface to be used to represent a collection of interfaces.
// This currently has no additional methods
type ErrorStackResults interface{}
// ErrorStack contains a list of ErrorStackResults that have been captured.
type ErrorStack []ErrorStackResults
// HasErrors checks if any errors exist
func (stack ErrorStack) HasErrors() bool {
return len(stack) > 0
}
// Error is string representation of an ErrorStack and displays
// the total number of errors. This is to implement the error type.
func (stack ErrorStack) Error() string {
var pretty string
for _, e := range stack {
pretty += fmt.Sprintf("%v\n", e)
}
return fmt.Sprintf("%d Errors: %s", len(stack), pretty)
}
// Result creates a struct that is properly formatted for the client-validation.
func (stack ErrorStack) Result(fieldname string) interface{} {
return struct {
Fieldname string
Errors ErrorStack
}{
fieldname, stack,
}
}
// Append adds an error to the list of errors. For those that implement the ValidationResult interface,
// the return value for Result(fieldname) will be used to populate the stack. This is to properly format
// error/validation information that is to be returned to the client-side.
func (stack *ErrorStack) Append(fieldname string, err error) {
if err == nil {
return
}
switch t := err.(type) {
case ValidationResult:
*stack = append(*stack, t.Result(fieldname))
default:
fmt.Printf("Error [%v] has no Result(string) field implemented\n", err)
*stack = append(*stack, err)
}
}
// NewErrorStack creates a new stack of errors for a particular field.
func NewErrorStack(fieldname string, err error) ErrorStack {
var stack ErrorStack
stack.Append(fieldname, err)
return stack
}
// ErrFieldInvalid represents an error for a field with an invalid value.
type ErrFieldInvalid struct {
Message string
}
// Error returns the string representation of a Field Invalid error.
func (e ErrFieldInvalid) Error() string {
return e.Message
}
// Result creates a struct that is properly formatted for the client-side validation.
func (e ErrFieldInvalid) Result(fieldname string) interface{} {
return struct {
Fieldname string
Error string
}{
fieldname,
e.Error(),
}
}
// ErrFieldRequired represents an error for a field that requires data.
type ErrFieldRequired struct {
Message string
}
// Error is a basic representation of a Require Field error.
func (e ErrFieldRequired) Error() string {
return e.Message
}
// Result creates a struct that is properly formatted for the client-side validation.
func (e ErrFieldRequired) Result(fieldname string) interface{} {
return struct {
Fieldname string
Error string
}{
fieldname,
e.Error(),
}
}
// ErrInvalidLocation represents an error for location information with additional options.
type ErrInvalidLocation struct {
Message string
Suggestions []GeocodeResult
}
// Error returns the error message.
func (e ErrInvalidLocation) Error() string {
return e.Message
}
// Result creates a struct that is properly formatted for the client-side validation.
func (e ErrInvalidLocation) Result(fieldname string) interface{} {
return struct {
Fieldname string
Error string
Suggestions []GeocodeResult
}{
fieldname,
e.Error(),
e.Suggestions,
}
}