mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
database/sql: make Rows.Scan properly wrap underlying errors
The prior implementation used the format verb %v which unfortunately improperly wrapped any underlying scanner errors, and we couldn't use errors.Is nor errors.As. This change fixes that by using the %w verb. Added a unit to ensure that both error sub string matching works, but also that errors.Is works as expected. Fixes #38099 Change-Id: Iea667041dd8081d961246f77f2542330417292dc Reviewed-on: https://go-review.googlesource.com/c/go/+/248337 Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com> Reviewed-by: Daniel Theophanes <kardianos@gmail.com> Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
d0d6593d1d
commit
a20cb4ca5c
2 changed files with 39 additions and 1 deletions
|
|
@ -4149,6 +4149,41 @@ func TestQueryExecContextOnly(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
type alwaysErrScanner struct{}
|
||||
|
||||
var errTestScanWrap = errors.New("errTestScanWrap")
|
||||
|
||||
func (alwaysErrScanner) Scan(interface{}) error {
|
||||
return errTestScanWrap
|
||||
}
|
||||
|
||||
// Issue 38099: Ensure that Rows.Scan properly wraps underlying errors.
|
||||
func TestRowsScanProperlyWrapsErrors(t *testing.T) {
|
||||
db := newTestDB(t, "people")
|
||||
defer closeDB(t, db)
|
||||
|
||||
rows, err := db.Query("SELECT|people|age|")
|
||||
if err != nil {
|
||||
t.Fatalf("Query: %v", err)
|
||||
}
|
||||
|
||||
var res alwaysErrScanner
|
||||
|
||||
for rows.Next() {
|
||||
err = rows.Scan(&res)
|
||||
if err == nil {
|
||||
t.Fatal("expecting back an error")
|
||||
}
|
||||
if !errors.Is(err, errTestScanWrap) {
|
||||
t.Fatalf("errors.Is mismatch\n%v\nWant: %v", err, errTestScanWrap)
|
||||
}
|
||||
// Ensure that error substring matching still correctly works.
|
||||
if !strings.Contains(err.Error(), errTestScanWrap.Error()) {
|
||||
t.Fatalf("Error %v does not contain %v", err, errTestScanWrap)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// badConn implements a bad driver.Conn, for TestBadDriver.
|
||||
// The Exec method panics.
|
||||
type badConn struct{}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue