Skip to content

database/sql: wrap errors with %w in driverArgsConnLocked #64728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/database/sql/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func driverArgsConnLocked(ci driver.Conn, ds *driverStmt, args []any) ([]driver.
}
goto nextCheck
default:
return nil, fmt.Errorf("sql: converting argument %s type: %v", describeNamedValue(nv), err)
return nil, fmt.Errorf("sql: converting argument %s type: %w", describeNamedValue(nv), err)
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/database/sql/driver/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ type ValueConverter interface {

// Valuer is the interface providing the Value method.
//
// Errors returned by the [Value] method are wrapped by the database/sql package.
// This allows callers to use [errors.Is] for precise error handling after operations
// like [database/sql.Query], [database/sql.Exec], or [database/sql.QueryRow].
//
// Types implementing Valuer interface are able to convert
// themselves to a driver [Value].
type Valuer interface {
Expand Down
43 changes: 43 additions & 0 deletions src/database/sql/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4398,6 +4398,49 @@ func TestRowsScanProperlyWrapsErrors(t *testing.T) {
}
}

type alwaysErrValuer struct{}

// errEmpty is returned when an empty value is found
var errEmpty = errors.New("empty value")

func (v alwaysErrValuer) Value() (driver.Value, error) {
return nil, errEmpty
}

// Issue 64707: Ensure that Stmt.Exec and Stmt.Query properly wraps underlying errors.
func TestDriverArgsWrapsErrors(t *testing.T) {
db := newTestDB(t, "people")
defer closeDB(t, db)

t.Run("exec", func(t *testing.T) {
_, err := db.Exec("INSERT|keys|dec1=?", alwaysErrValuer{})
if err == nil {
t.Fatal("expecting back an error")
}
if !errors.Is(err, errEmpty) {
t.Fatalf("errors.Is mismatch\n%v\nWant: %v", err, errEmpty)
}
// Ensure that error substring matching still correctly works.
if !strings.Contains(err.Error(), errEmpty.Error()) {
t.Fatalf("Error %v does not contain %v", err, errEmpty)
}
})

t.Run("query", func(t *testing.T) {
_, err := db.Query("INSERT|keys|dec1=?", alwaysErrValuer{})
if err == nil {
t.Fatal("expecting back an error")
}
if !errors.Is(err, errEmpty) {
t.Fatalf("errors.Is mismatch\n%v\nWant: %v", err, errEmpty)
}
// Ensure that error substring matching still correctly works.
if !strings.Contains(err.Error(), errEmpty.Error()) {
t.Fatalf("Error %v does not contain %v", err, errEmpty)
}
})
}

func TestContextCancelDuringRawBytesScan(t *testing.T) {
for _, mode := range []string{"nocancel", "top", "bottom", "go"} {
t.Run(mode, func(t *testing.T) {
Expand Down