forked from goji/param
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
129 lines (111 loc) · 3.69 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
package param
import (
"fmt"
"reflect"
)
// ValueError is an error type returned when param has difficulty deserializing
// a parameter value.
type ValueError struct {
// The key that was in error.
Key string
// The type that was expected.
Type reflect.Type
// The underlying error produced as part of the deserialization process,
// if one exists.
Err error
}
func (v ValueError) Error() string {
return fmt.Sprintf("param: error parsing key %q as %v: %v",
v.Key, v.Type, v.Err)
}
// SingletonError is an error type returned when a parameter is passed multiple
// times when only a single value is expected. For example, for a struct with
// integer field "foo", "foo=1&foo=2" will return a SingletonError with key
// "foo".
type SingletonError struct {
// The key that was in error.
Key string
// The type that was expected for that key.
Type reflect.Type
// The list of values that were provided for that key.
Values []string
}
func (s SingletonError) Error() string {
return fmt.Sprintf("param: error parsing key %q: expected single "+
"value but was given %d: %v", s.Key, len(s.Values), s.Values)
}
// NestingError is an error type returned when a key is nested when the target
// type does not support nesting of the given type. For example, deserializing
// the parameter key "anint[foo]" into a struct that defines an integer param
// "anint" will produce a NestingError with key "anint" and nesting "[foo]".
type NestingError struct {
// The portion of the key that was correctly parsed into a value.
Key string
// The type of the key that was invalidly nested on.
Type reflect.Type
// The portion of the key that could not be parsed due to invalid
// nesting.
Nesting string
}
func (n NestingError) Error() string {
return fmt.Sprintf("param: error parsing key %q: invalid nesting "+
"%q on %s key %q", n.Key+n.Nesting, n.Nesting, n.Type, n.Key)
}
// SyntaxErrorSubtype describes what sort of syntax error was encountered.
type SyntaxErrorSubtype int
const (
MissingOpeningBracket SyntaxErrorSubtype = iota + 1
MissingClosingBracket
)
// SyntaxError is an error type returned when a key is incorrectly formatted.
type SyntaxError struct {
// The key for which there was a syntax error.
Key string
// The subtype of the syntax error, which describes what sort of error
// was encountered.
Subtype SyntaxErrorSubtype
// The part of the key (generally the suffix) that was in error.
ErrorPart string
}
func (s SyntaxError) Error() string {
prefix := fmt.Sprintf("param: syntax error while parsing key %q: ",
s.Key)
switch s.Subtype {
case MissingOpeningBracket:
return prefix + fmt.Sprintf("expected opening bracket, got %q",
s.ErrorPart)
case MissingClosingBracket:
return prefix + fmt.Sprintf("expected closing bracket in %q",
s.ErrorPart)
default:
panic("switch is not exhaustive!")
}
}
// KeyError is an error type returned when an unknown field is set on a struct.
type KeyError struct {
// The full key that was in error.
FullKey string
// The key of the struct that did not have the given field.
Key string
// The type of the struct that did not have the given field.
Type reflect.Type
// The name of the field which was not present.
Field string
}
func (k KeyError) Error() string {
return fmt.Sprintf("param: error parsing key %q: unknown field %q on "+
"struct %q of type %v", k.FullKey, k.Field, k.Key, k.Type)
}
// InvalidParseError describes an invalid argument passed to Parse. It is always
// the result of programmer error.
type InvalidParseError struct {
Type reflect.Type
Hint string
}
func (err InvalidParseError) Error() string {
msg := fmt.Sprintf("param/parse: unsupported type %v", err.Type)
if err.Hint != "" {
msg += ": " + err.Hint
}
return msg
}