Skip to content

database/sql: Rows.Scan does not use the %w verb when wrapping errors #38099

Closed
@sfllaw

Description

@sfllaw

What version of Go are you using (go version)?

$ go version
go version go1.13.4 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/slaw/.cache/go-build"
GOENV="/home/slaw/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/slaw/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.13"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.13/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build400385127=/tmp/go-build -gno-record-gcc-switches"
GOROOT/bin/go version: go version go1.13.4 linux/amd64
GOROOT/bin/go tool compile -V: compile version go1.13.4
uname -sr: Linux 5.3.0-40-generic
Distributor ID:	Ubuntu
Description:	Ubuntu 19.10
Release:	19.10
Codename:	eoan
/lib/x86_64-linux-gnu/libc.so.6: GNU C Library (Ubuntu GLIBC 2.30-0ubuntu2.1) stable release version 2.30.
gdb --version: GNU gdb (Ubuntu 8.3-0ubuntu1) 8.3

What did you do?

database/sql.Rows.Scan does not use the %w verb when wrapping errors:

return fmt.Errorf(`sql: Scan error on column index %d, name %q: %v`, i, rs.rowsi.Columns()[i], err)

Here is a motivating example involving a custom type that implements database/sql.Scanner:

package main

import (
	"database/sql"
	"errors"
	"fmt"

	_ "github.com/mattn/go-sqlite3"
)

var ErrInvalid = errors.New("not an ASCII string")

type ASCII []byte

func (a ASCII) Scan(src interface{}) error {
	// Elide code that parses src into a.
	return ErrInvalid
}

func main() {
	db, err := sql.Open("sqlite3", ":memory:")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	var a ASCII
	if err := db.QueryRow(`SELECT "世界"`).Scan(&a); err != nil {
		if !errors.Is(err, ErrInvalid) {
			panic(fmt.Errorf("unexpected error: %w", err))
		}
		fmt.Printf("err is ErrInvalid: %v\n", err)
	}
}

What did you expect to see?

sfllaw@nyancat:~/go-sql-error-bug$ go run -trimpath main.go
err is ErrInvalid: sql: Scan error on column index 0, name "\"世界\"": not an ASCII string

What did you see instead?

sfllaw@nyancat:~/go-sql-error-bug$ go run -trimpath main.go
panic: unexpected error: sql: Scan error on column index 0, name "\"世界\"": not an ASCII string

goroutine 1 [running]:
main.main()
	github.com/sfllaw/go-sql-error-bug@/main.go:30 +0x2f0
exit status 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.early-in-cycleA change that should be done early in the 3 month dev cycle.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions