Skip to content

Commit f88791f

Browse files
committed
feat: make DecodeError exported
Closes: #48
1 parent 6bfc75a commit f88791f

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

decoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,15 +430,15 @@ func wrapDecodeError(r Reader, field string, fieldIndex int, err error) error {
430430
FieldPos(fieldIndex int) (line, column int)
431431
})
432432
if !ok {
433-
return &decodeError{
433+
return &DecodeError{
434434
Field: field,
435435
Err: err,
436436
}
437437
}
438438

439439
l, c := fp.FieldPos(fieldIndex)
440440

441-
return &decodeError{
441+
return &DecodeError{
442442
Field: field,
443443
Line: l,
444444
Column: c,

error.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,25 +155,39 @@ func (e *MissingColumnsError) Error() string {
155155
return b.String()
156156
}
157157

158-
// decodeError provides context to decoding errors if available.
158+
// DecodeError provides context to decoding errors if available.
159159
//
160160
// The caller should use errors.As in order to fetch the underlying error if
161161
// needed.
162-
type decodeError struct {
163-
Field string
164-
Line int
162+
//
163+
// Some of the DecodeError's fields are only populated if the Reader supports
164+
// PosField method. Specifically Line and Column. FieldPos is available in
165+
// csv.Reader since Go1.17.
166+
type DecodeError struct {
167+
// Field describes the struct's tag or field name on which the error happened.
168+
Field string
169+
170+
// Line is 1-indexed line number taken from FieldPost method. It is only
171+
// available if the used Reader supports FieldPos method.
172+
Line int
173+
174+
// Column is 1-indexed column index taken from FieldPost method. It is only
175+
// available if the used Reader supports FieldPos method.
165176
Column int
166-
Err error
177+
178+
// Error is the actual error that was returned while attempting to decode
179+
// a field.
180+
Err error
167181
}
168182

169-
func (e *decodeError) Error() string {
183+
func (e *DecodeError) Error() string {
170184
if e.Line > 0 && e.Column > 0 {
171185
// Lines and Columns are 1-indexed so this check is fine.
172186
return fmt.Sprintf("%s: field %q line %d column %d", e.Err, e.Field, e.Line, e.Column)
173187
}
174188
return fmt.Sprintf("%s: field %q", e.Err, e.Field)
175189
}
176190

177-
func (e *decodeError) Unwrap() error {
191+
func (e *DecodeError) Unwrap() error {
178192
return e.Err
179193
}

0 commit comments

Comments
 (0)