-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.go
157 lines (130 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package null
import (
"fmt"
"reflect"
"strings"
)
// MarshalError is returned if marshaling is not successful.
type MarshalError struct {
// prefix indicates the error class
prefix string
// SrcValue holds the value that couldn't be marshaled
SrcValue interface{}
// SrcType holds the name of SrcValue's type
SrcType string
}
// helper function to build a MarshalError.
func makeMarshalError(prefix string, src interface{}) error {
return MarshalError{
prefix: prefix,
SrcValue: src,
SrcType: reflect.TypeOf(src).Name(),
}
}
// Error returns a string representation of e.
func (e MarshalError) Error() string {
return fmt.Sprintf(
"%s: cannot marshal %v of type %s",
e.prefix, e.SrcValue, e.SrcType,
)
}
// UnmarshalError is returned if unmarshaling is not successful.
type UnmarshalError struct {
// prefix indicates the error class
prefix string
// SrcValue holds the data that couldn't be unmarshaled
SrcValue string
// DestType holds the destination type
DestType string
}
// helper function to build a UnmarshalError.
func makeUnmarshalError(prefix string, src []byte, dest interface{}) error {
return UnmarshalError{
prefix: prefix,
SrcValue: string(src),
DestType: reflect.TypeOf(dest).Name(),
}
}
// Error returns a string representation of e.
func (e UnmarshalError) Error() string {
return fmt.Sprintf(
"%s: cannot unmarshal '%s' into Go value of type %s",
e.prefix, e.SrcValue, e.DestType,
)
}
// ConversionError is returned when a type conversion cannot happen without
// data loss.
type ConversionError struct {
// prefix indicates the error class
prefix string
// SrcValue holds the value that couldn't be converted
SrcValue interface{}
// SrcType holds the name of SrcValue's type
SrcType string
// DestType holds the destination type
DestType string
}
// helper function to build a ConversionError.
func makeConversionError(prefix string, src, dest interface{}) error {
return ConversionError{
prefix: prefix,
SrcValue: src,
SrcType: reflect.TypeOf(src).Name(),
DestType: reflect.TypeOf(dest).Name(),
}
}
// Error returns a string representation of e.
func (e ConversionError) Error() string {
return fmt.Sprintf(
"%s: cannot convert %v of type %s into Go value of type %s",
e.prefix, e.SrcValue, e.SrcType, e.DestType,
)
}
// ParseError is returned when a string cannot be parsed.
type ParseError struct {
// prefix indicates the error class
prefix string
// SrcString holds the string that couldn't be parsed
SrcString string
// DestType holds the destination type
DestType string
}
// helper function to build a ParseError.
func makeParseError(prefix, src string, dest interface{}) error {
return ParseError{
prefix: prefix,
SrcString: src,
DestType: reflect.TypeOf(dest).Name(),
}
}
// Error returns a string representation of e.
func (e ParseError) Error() string {
return fmt.Sprintf(
"%s: cannot parse '%s' into Go value of type %s",
e.prefix, e.SrcString, e.DestType,
)
}
// TypeError is returned when a value of the wrong type is supplied.
type TypeError struct {
// prefix indicates the error class
prefix string
// InvalidType holds the name of the type that is not valid
InvalidType string
// ExpectedTypes holds the names of accepted types
ExpectedTypes []string
}
// helper function to build a TypeError.
func makeTypeError(prefix string, src interface{}, exp ...string) error {
return TypeError{
prefix: prefix,
InvalidType: reflect.TypeOf(src).Name(),
ExpectedTypes: exp,
}
}
// Error returns a string representation of e.
func (e TypeError) Error() string {
return fmt.Sprintf(
"%s: invalid type (expected %s, got %s)",
e.prefix, strings.Join(e.ExpectedTypes, " or "), e.InvalidType,
)
}