News for hackers. Stuff that matters.
Re:Lies we tell ourselves to keep using Golang by Cthulhu_ on Tuesday November 26, 2024 @05:57AM (#42244579)
This is down to developer style and agreements though; Go has typed errors and a set of utilities to match them [0]. Not using those is a choice, just like how in Java you can just `catch (Exception e)` after calling a dozen methods that might each throw a different exception.

[0] https://pkg.go.dev/errors

Re:Lies we tell ourselves to keep using Golang by Yoric on Tuesday November 26, 2024 @07:57AM (#42245302)
Interestingly, every time (and I mean _every_ time) that I've tried to use `errors.As` on errors raised by lib code, I found out that the lib just went with "nah, I'm just going to use `errors.New` or `fmt.Errorf`", which makes the error impossible to match.

So... I'd say that this is a fumble in the design of Go.

Re:Lies we tell ourselves to keep using Golang by wbl on Tuesday November 26, 2024 @02:21PM (#42248974)
%W exists to solve this
Re:Lies we tell ourselves to keep using Golang by the_gipsy on Tuesday November 26, 2024 @05:53PM (#42251018)
How? Stringly matching? That's not typesafe at all.
Re:Lies we tell ourselves to keep using Golang by wbl on Tuesday November 26, 2024 @09:12PM (#42252263)
No it wraps the underlying error
Re:Lies we tell ourselves to keep using Golang by the_gipsy on Wednesday November 27, 2024 @05:40AM (#42254899)
But the underlying error stays unmatchable. Doesn't sound like a solution if you have to duplicate every error type, and worse, they don't even map 1:1 but now you have the same underlying error wrapped to god knows how many different errors.

For example, the lib produces some an error "bad file descriptor". You'll be wrapping it when you call fileOpen, fileDelete, etc etc 20 times. So you will be wrapping it in "open error", "delete error", etc, 20 times. You cannot try to match it to "bad file descriptor", that information is lost, you now have 20 relatively useless errors.

Except if you stringly match.

Re:Lies we tell ourselves to keep using Golang by wbl on Wednesday November 27, 2024 @09:18AM (#42256266)
https://pkg.go.dev/errors#Is and https://pkg.go.dev/fmt#Errorf clearly state that there is a way to match these errors if the package exposes the values, which the stdlib does.
Re:Lies we tell ourselves to keep using Golang by the_gipsy on Wednesday November 27, 2024 @02:07PM (#42258740)
Ah okay, it seems to work because errors are pointers, and errors.Is just checks for equality.
Re:Lies we tell ourselves to keep using Golang by Yoric on Wednesday November 27, 2024 @02:49PM (#42259098)
Which only works when the error itself doesn't contain any information.

It's better than nothing, of course.

Re:Lies we tell ourselves to keep using Golang by Yoric on Wednesday November 27, 2024 @07:47AM (#42255634)
It would, if people actually defined new errors. But, as I mentioned in my message, people just use `errors.New` or `fmt.Errorf`, all the way down, so you end up with errors that cannot be matched. Which means, in turn, that if these are errors that should be handled properly (and not just propagated/logged), they cannot.