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

errors: add example around unwrapping errors.Join #69586

Closed
tpaschalis opened this issue Sep 23, 2024 · 4 comments
Closed

errors: add example around unwrapping errors.Join #69586

tpaschalis opened this issue Sep 23, 2024 · 4 comments

Comments

@tpaschalis
Copy link
Contributor

tpaschalis commented Sep 23, 2024

Go version

go version go1.21.6 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/tpaschalis/.cache/go-build'
GOENV='/home/tpaschalis/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/tpaschalis/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/tpaschalis/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.6'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/tpaschalis/Workspace/lox-go/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3067468203=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I used errors.Join to group a bunch of errors together, but then realized that getting the individual entries back is not straightforward (unless I'm missing something); the ways I could spot to get the errors back would be to use errors.As or an inline cast to Unwrap() []error.

Should we include an example that shows people to handle this task to set some best practices/precedence?

https://go.dev/play/p/G5vw-xdqBvm

What did you see happen?

The errors package documentation states:

An error e wraps another error if e's type has one of the methods:
Unwrap() error
Unwrap() []error

If e.Unwrap() returns a non-nil error w or a slice containing w, then we say that e wraps w.
**A nil error returned from e.Unwrap() indicates that e does not wrap any error. **

It also states that

Join returns an error that wraps the given errors. Any nil error values are discarded.
[...]
A non-nil error returned by Join implements the Unwrap() []error method.

and that

Unwrap only calls a method of the form "Unwrap() error". In particular Unwrap does not unwrap errors returned by Join

I think that the two following statements are contradictory since calling errors.Unwrap(errors.Join(err1, err2)) will return nil, even though we said that Join returns a wrapped error. The first statement is only true for a kind of wrapped errors.

A nil error returned from e.Unwrap() indicates that e does not wrap any error
Join returns an error that wraps the given errors

What did you expect to see?

An documented way to get the individual errors back instead of fiddling to find the right interface to cast to.

@seankhliao
Copy link
Member

see #57358
Is and As are the intended ways to interact with errors.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Sep 23, 2024
@tpaschalis
Copy link
Contributor Author

@seankhliao I'm not proposing a new way of interacting with these errors, but adding an example with errors.As and joined errors so that it's easier for people to get the individual items.

Would you object to that?

@ianlancetaylor
Copy link
Member

I think the example would be OK if you want to send it. No issue is required. See https://go.dev/doc/contribute. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants