-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy patherror.go
179 lines (148 loc) · 4.6 KB
/
error.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package csvutil
import (
"bytes"
"errors"
"fmt"
"reflect"
"strconv"
)
// ErrFieldCount is returned when header's length doesn't match the length of
// the read record.
var ErrFieldCount = errors.New("wrong number of fields in record")
// An UnmarshalTypeError describes a string value that was not appropriate for
// a value of a specific Go type.
type UnmarshalTypeError struct {
Value string // string value
Type reflect.Type // type of Go value it could not be assigned to
}
func (e *UnmarshalTypeError) Error() string {
return "csvutil: cannot unmarshal " + strconv.Quote(e.Value) + " into Go value of type " + e.Type.String()
}
// An UnsupportedTypeError is returned when attempting to encode or decode
// a value of an unsupported type.
type UnsupportedTypeError struct {
Type reflect.Type
}
func (e *UnsupportedTypeError) Error() string {
if e.Type == nil {
return "csvutil: unsupported type: nil"
}
return "csvutil: unsupported type: " + e.Type.String()
}
// An InvalidDecodeError describes an invalid argument passed to Decode.
// (The argument to Decode must be a non-nil struct pointer)
type InvalidDecodeError struct {
Type reflect.Type
}
func (e *InvalidDecodeError) Error() string {
if e.Type == nil {
return "csvutil: Decode(nil)"
}
if e.Type.Kind() != reflect.Ptr {
return "csvutil: Decode(non-pointer " + e.Type.String() + ")"
}
typ := walkType(e.Type)
switch typ.Kind() {
case reflect.Struct:
case reflect.Slice, reflect.Array:
if typ.Elem().Kind() != reflect.Struct {
return "csvutil: Decode(invalid type " + e.Type.String() + ")"
}
default:
return "csvutil: Decode(invalid type " + e.Type.String() + ")"
}
return "csvutil: Decode(nil " + e.Type.String() + ")"
}
// An InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
// (The argument to Unmarshal must be a non-nil slice of structs pointer)
type InvalidUnmarshalError struct {
Type reflect.Type
}
func (e *InvalidUnmarshalError) Error() string {
if e.Type == nil {
return "csvutil: Unmarshal(nil)"
}
if e.Type.Kind() != reflect.Ptr {
return "csvutil: Unmarshal(non-pointer " + e.Type.String() + ")"
}
return "csvutil: Unmarshal(invalid type " + e.Type.String() + ")"
}
// InvalidEncodeError is returned by Encode when the provided value was invalid.
type InvalidEncodeError struct {
Type reflect.Type
}
func (e *InvalidEncodeError) Error() string {
if e.Type == nil {
return "csvutil: Encode(nil)"
}
return "csvutil: Encode(" + e.Type.String() + ")"
}
// InvalidMarshalError is returned by Marshal when the provided value was invalid.
type InvalidMarshalError struct {
Type reflect.Type
}
func (e *InvalidMarshalError) Error() string {
if e.Type == nil {
return "csvutil: Marshal(nil)"
}
if walkType(e.Type).Kind() == reflect.Slice {
return "csvutil: Marshal(non struct slice " + e.Type.String() + ")"
}
if walkType(e.Type).Kind() == reflect.Array {
return "csvutil: Marshal(non struct array " + e.Type.String() + ")"
}
return "csvutil: Marshal(invalid type " + e.Type.String() + ")"
}
// MarshalerError is returned by Encoder when MarshalCSV or MarshalText returned
// an error.
type MarshalerError struct {
Type reflect.Type
MarshalerType string
Err error
}
func (e *MarshalerError) Error() string {
return "csvutil: error calling " + e.MarshalerType + " for type " + e.Type.String() + ": " + e.Err.Error()
}
// Unwrap implements Unwrap interface for errors package in Go1.13+.
func (e *MarshalerError) Unwrap() error {
return e.Err
}
func errPtrUnexportedStruct(typ reflect.Type) error {
return fmt.Errorf("csvutil: cannot decode into a pointer to unexported struct: %s", typ)
}
// MissingColumnsError is returned by Decoder only when DisallowMissingColumns
// option was set to true. It contains a list of all missing columns.
type MissingColumnsError struct {
Columns []string
}
func (e *MissingColumnsError) Error() string {
var b bytes.Buffer
b.WriteString("csvutil: missing columns: ")
for i, c := range e.Columns {
if i > 0 {
b.WriteString(", ")
}
fmt.Fprintf(&b, "%q", c)
}
return b.String()
}
// decodeError provides context to decoding errors if available.
//
// The caller should use errors.As in order to fetch the underlying error if
// needed.
type decodeError struct {
Field string
Line int
Column int
Err error
}
func (e *decodeError) Error() string {
if e.Line > 0 && e.Column > 0 {
// Lines and Columns are 1-indexed so this check is fine.
return fmt.Sprintf("%s: field %q line %d column %d", e.Err, e.Field, e.Line, e.Column)
}
return fmt.Sprintf("%s: field %q", e.Err, e.Field)
}
func (e *decodeError) Unwrap() error {
return e.Err
}