Hacker News new | ask | show | jobs
by binwiederhier 2609 days ago
As others have stated, this seems incredibly odd to me:

> If the last argument is an error and the format string ends with ": %w", ...

This seems like a magic-string kinda hack to me. I like the idea of wrapping errors so that you keep the stack and full context, especially since you may need additional structured data from all errors (e.g. DB error, access error, ...) to produce user facing messages, so IMHO the wrapping should be more explicit than just %w.

2 comments

It is. It's a backwards-compatibility magic string hack. I would suggest new code uses the new, formal ways of obtaining the same result.

(I'm not defending it so much as explaining it. I'm not sure how I feel about it myself.)

Backwards compatibility with what? You have to change the code to do anything new, that change could be using a different function entirely.
See my apology below. I was wrong.
> I would suggest new code uses the new, formal ways of obtaining the same result

I missed that. How else do you wrap an error with xerrors?

    return errors.Wrap(err, "read failed")
That is not part of xerrors at this point. Rationale: https://github.com/golang/go/issues/29934#issuecomment-46420...

Personally, I don't care for "string magic"

Edit: Instead of making another post, I'll add this here. It also feels odd that concerns about xerrors.As possibly panicking at runtime are considered addressed by the addition of a go vet check: https://github.com/golang/go/issues/29934#issuecomment-46252...

I am not opposed to go vet, to be clear, but one of the aspects of the go compiler is that it does not warn, only errors out if compilation can't proceed. But the design of xerrors.As is fine because go vet acts like a compiler warning might? This is an aesthetic complaint more than anything else, but it still doesn't sit right with me.

Thank you, my bad!
That's what I do now with pkg/errors, but I don't see a xerrors.Wrap in the docs or any mention of it in the proposal.
My apologies, I seem to have confused the two packages. I don't see it in the godoc either. In which case I echo your desire to have an explicit method of formatting and wrapping.

I suppose it won't take long for a component of one of the gometalinter or golang-ci or whatever to develop a "Errorf used without %w in the final position" warning, which will be good enough for me, but it would be better to have something like Wrap officially, IMHO.

Yes, it's a magic string hack. On the other hand, it's concise and easily learned. This is kind of the opposite of the "if err!=nil" dance that many people complain about.

I expect we will get used to it pretty quickly.