Skip to content
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

For *csv.ParseError, error.Is() comes back false and error.As() comes back true? #42004

Closed
coreyog opened this issue Oct 15, 2020 · 1 comment

Comments

@coreyog
Copy link

coreyog commented Oct 15, 2020

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

$ go version
go version go1.15.3 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/corey/.cache/go-build"
GOENV="/home/corey/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/corey/go/pkg/mod"
GONOPROXY="gitlab.com/ethopass/*"
GONOSUMDB="gitlab.com/ethopass/*"
GOOS="linux"
GOPATH="/home/corey/go"
GOPRIVATE="gitlab.com/ethopass/*"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/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-build937491866=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/SdpH9lJMVu1

I setup a reader that expects 2 fields per record and gave it some data with only 1 field. The csv.Reader returns a *csv.ParseError that contians the details about not containing enough fields.

When comparing against an empty *csv.ParseError, errors.Is() returns false and errors.As() returns true.

What did you expect to see?

I had the understanding that errors.Is and errors.As would return the same bool values when passed similar parameters indicating that the error is of that type. Either both would return true or both would return false. In this setup, I expected errors.Is(err, &csv.ParseError{}) to return true because the error is indeed a *csv.ParseError.

What did you see instead?

Instead, I find that errors.Is() returns false and errors.As() returns true when given similar parameters.

@coreyog coreyog changed the title For *csv.ParseError, error.Is() comes back false and error.As() comes back true? For *csv.ParseError, error.Is() comes back false and error.As() comes back true? Oct 15, 2020
@ianlancetaylor
Copy link
Contributor

errors.Is and errors.As are different. Your example using errors.Is is basically asking "is the returned error an empty csv.ParseError? The answer is no; it is not an empty csv.ParseError, it is a csv.ParseError that includes some data. Your example using errors.As is saying: "give me this error in the form of a csv.ParseError", and that is what happens.

errors.Is is like ==, errors.As is like a type assertion.

Closing because this is working as intended.

@golang golang locked and limited conversation to collaborators Oct 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants