Skip to content

Commit bdd990b

Browse files
author
Jason Turim
committed
Include the field name in error messages when scanning structs
1 parent c2175fe commit bdd990b

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

rows.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func (rows *baseRows) Scan(dest ...any) error {
272272

273273
err := rows.scanPlans[i].Scan(values[i], dst)
274274
if err != nil {
275-
err = ScanArgError{ColumnIndex: i, Err: err}
275+
err = ScanArgError{ColumnIndex: i, FieldName: &fieldDescriptions[i].Name, Err: err}
276276
rows.fatal(err)
277277
return err
278278
}
@@ -334,11 +334,16 @@ func (rows *baseRows) Conn() *Conn {
334334

335335
type ScanArgError struct {
336336
ColumnIndex int
337+
FieldName *string
337338
Err error
338339
}
339340

340341
func (e ScanArgError) Error() string {
341-
return fmt.Sprintf("can't scan into dest[%d]: %v", e.ColumnIndex, e.Err)
342+
msg := fmt.Sprintf("can't scan into dest[%d]: %v", e.ColumnIndex, e.Err)
343+
if e.FieldName != nil {
344+
msg = fmt.Sprintf("can't scan into dest[%d] (%s): %v", e.ColumnIndex, *e.FieldName, e.Err)
345+
}
346+
return msg
342347
}
343348

344349
func (e ScanArgError) Unwrap() error {
@@ -366,7 +371,7 @@ func ScanRow(typeMap *pgtype.Map, fieldDescriptions []pgconn.FieldDescription, v
366371

367372
err := typeMap.Scan(fieldDescriptions[i].DataTypeOID, fieldDescriptions[i].Format, values[i], d)
368373
if err != nil {
369-
return ScanArgError{ColumnIndex: i, Err: err}
374+
return ScanArgError{ColumnIndex: i, FieldName: &fieldDescriptions[i].Name, Err: err}
370375
}
371376
}
372377

0 commit comments

Comments
 (0)