On Mon, 23 Mar 2020 at 11:22, Axel Wagner <axel.wagner...@googlemail.com> wrote:

> • A function takes an io.Reader to parse the stream into some data structure 
> (think a json-parser). It obviously uses no other input or abstraction or 
> state. It makes sense to wrap the error - this allows you to provide 
> structured extra information (like offsets) to the user of the library and 
> better error messages, while maintaining your users ability to switch on what 
> actually went wrong.

That's a good point, and I suppose it generalises.  Whenever you
receive an interace from client code, then you have no
compiler-enforced way to predict which  errors  you'll get from calls
to that interface; but your client might.  Therefore you should either
pass the error up directly or else user wrapping.

(Microsoft has/had the problem that many Windows APIs let clients
implement abstract classes for OS middleware to call.  The MSDN page
for each method would list which error codes were allowed for the
method, but they had no way to enforce this, so the middleware had to
try and handle all possible errors).

> That latter part, BTW, is IMO a blessing and a curse. It's a blessing, 
> because it can reduce coupling from details in the wrapping case. ...

I think this is the classic dynamic vs. static typing trade-off.  Go
has always been a mostly static language but happy to sprinkle in
dynamic typing stategically through interfaces and reflection.  By
standardising on the `error` interface, Go chose error handling as one
of those dynamic sprinkles.

> Sorry if this message is a bit chaotic and stream-of-consciousnes-y :) I 
> haven't quite figured out how to talk about all of this yet :)
> I guess the tl;dr is, that I tend to agree - don't just use %w without 
> thinking. Make a deliberate choice if you want to commit to this API detail. 
> And as usually in API design: If in doubt, prefer to start out with less 
> commitment, flexibility and surface.

So to turn this discussion into something actionable: should this
advice be added into the documentation?  And if so, where (in package
fmt or errors)?

Lots of programers will assume that because %w is new, then it must be
the Go team's recommended "best practice".  I think I was unconciously
thinking that until I read the blog post.



-- 
Adrian Ratnapala

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAN%2BHj7gvJru-ZoLUcdcCNde3iJgK5j6aoCbipksCRMYSMznjfw%40mail.gmail.com.

Reply via email to