News for hackers. Stuff that matters.
Re:Error handling in Go web apps shouldn't be so awkward by kitkat_new on Tuesday January 09, 2024 @09:03PM (#38935225)
well, no magic, goal reached.

However, the current implementation of error handling is atrocious.

If Go would take sum types and question marks from Rust, Go would keep the general error handling strategy and philosophy with the difference of being way less verbose and more ergonomic.

Re:Error handling in Go web apps shouldn't be so awkward by nonethewiser on Tuesday January 09, 2024 @09:18PM (#38935332)
> If Go would take sum types and question mark

Go is an incomplete language masquerading as a simple one.

Re:Error handling in Go web apps shouldn't be so awkward by MobiusHorizons on Tuesday January 09, 2024 @09:27PM (#38935387)
I get that you don’t like some of the features go has done without, but how do you justify incomplete? What do you even mean by that word? Clearly lots of code has been written in it. Its deficiencies do not prevent programs from being expressed. The only definition I can think of for complete would be Turing completeness, but that is clearly too low a bar for what you are saying.
Re:Error handling in Go web apps shouldn't be so awkward by kelnos on Wednesday January 10, 2024 @12:09AM (#38936464)
I personally am not a fan of Go, but I agree that it's a fine language that people are productive in.

Opinions about languages are going to vary a lot, and I don't think it's productive to debate this kind of thing.

I prefer languages with stronger, more expressive type systems, but I'll freely admit that I code slower (though usually more correctly) in languages like that. Some people prefer languages where they can easily hold all the language's syntax and rules and quirks in their head, and fire out code quickly.

All of that is fine. There's no objective truth there. I agree with the GP that Go feels incomplete. Before generics, it felt embarrassingly incomplete. But if you're fine and happy and productive with it as it is, that's great!

Re:Error handling in Go web apps shouldn't be so awkward by danmur on Tuesday January 09, 2024 @11:03PM (#38935998)
I think it's more a sign that Go will eventually pick up (some of/all of) the features people complain of it missing, in time. Maybe when it's a complete language it will still be simple; maybe trying to keep it artificially simple now will hamper it later. Remains to be seen.
Re:Error handling in Go web apps shouldn't be so awkward by bananapub on Tuesday January 09, 2024 @09:35PM (#38935432)
> but how do you justify incomplete

are you seriously suggesting that Go's insistence of requiring the programmer to scatter "if err != nil {}" after most function calls - with no compiler assistance if you forget - is complete either in the sense of "having all necessary parts, elements, or steps" or "being finished"?

it's terrible, causes bugs and it seems incredibly unlikely to me that it won't be improved.

Re:Error handling in Go web apps shouldn't be so awkward by randomdata on Tuesday January 09, 2024 @10:55PM (#38935940)
At very least, unless you hate other developers for some reason, you need to document in your test suite what errors are returned.

How, exactly, are going to forget a necessary `if err != nil` without your test suite blowing up?

If you do hate other developers for some reason, why not leave out some `err != nil`s to really drive home your hatred towards them?

Re:Error handling in Go web apps shouldn't be so awkward by kelnos on Wednesday January 10, 2024 @12:13AM (#38936487)
If you can forget to check an error at a call site, then you can just as easily forget to add a proper test for that error condition. I prefer writing in languages where the syntax and standard library and compiler will help me avoid mistakes, and not have to rely on tests -- more error-prone code I have to write! -- to do that for me (and yes, I understand that I will never avoid having to write some tests, but the more the language and compiler can do for me, the better).

A language with generics and sum types (so you can implement something like Either, Try, or Result) doesn't let you use the result of a fallible function call without handling the error explicitly. And if the result is unit/void, most compilers/linters will still warn you if you haven't used the Either/Try/Result type that's returned.

But ultimately it doesn't matter. Write in whatever language you feel comfortable and most productive.

Well, unless you're starting a new security-sensitive project and want to write in C. Just... stop.

Re:Error handling in Go web apps shouldn't be so awkward by randomdata on Wednesday January 10, 2024 @12:26AM (#38936568)
I don't follow. Errors are part of your functional specification. Not even a complete type system can avoid you needing to write tests to document the error conditions. We are not talking about writing tests to stand in where an incomplete type system may be lacking.

Assuming you don't hate other developers, and given the tests you would write in any language, how do you foresee missing an error branch? Again, not specifically trying to test for something that a sufficiently advanced type system would catch, just your regular level documenting that demonstrates to other developers that you don't hate them.

> then you can just as easily forget to add a proper test for that error condition

If an error condition is easily forgotten in the documentation, then who cares about the implementation thereof? It is obviously not important. In fact, it is arguable that your program is incorrect if you do handle the condition in such a case.

Re:Error handling in Go web apps shouldn't be so awkward by bananapub on Wednesday January 10, 2024 @02:42AM (#38937369)
what tool do you use to verify that every possible path your program, including all errors paths in dependencies, is executed?
Re:Error handling in Go web apps shouldn't be so awkward by randomdata on Wednesday January 10, 2024 @11:37AM (#38940422)
If you are referring to test coverage, the standard Go toolchain includes that out of the box.

But in this case you don't need to know about what paths have been executed. It will be obvious that an error path was not handled when your program does not adhere to function that is documented.

Re:Error handling in Go web apps shouldn't be so awkward by tripleo1 on Thursday January 11, 2024 @06:32PM (#38961134)
> including all errors paths in dependencies
Re:Error handling in Go web apps shouldn't be so awkward by Conscat on Tuesday January 09, 2024 @10:29PM (#38935772)
Goland and Emacs (with Tree Sitter) can at least generate those automatically for you so they won't be forgotten, fwiw.
Re:Error handling in Go web apps shouldn't be so awkward by im_dario on Wednesday January 10, 2024 @09:15AM (#38939216)
No "compiler assistance" doesn't mean no assistance at all. There is a growing set of linters based on the community-accepted analysis package [0] that prevent this and other gotchas that Go devs may be prone to.

It isn't a perfect solution, but it works at community level without needing to wait for the Go team to take care of or changing the language/toolchain.

0: https://pkg.go.dev/golang.org/x/tools/go/analysis

Re:Error handling in Go web apps shouldn't be so awkward by saturn_vk on Wednesday January 10, 2024 @10:02AM (#38939627)
> if you forget

Does this happen? Is there any data on how often error checking is forgotten?

Re:Error handling in Go web apps shouldn't be so awkward by fl0ki on Wednesday January 10, 2024 @02:11PM (#38941967)
In my experience, outright forgetting is very rare because an unused err should be a warning, but the following is not nearly as rare as it should be:

if err != nil { return nil }

With how boilerplate the correct version is, you learn to skip over it entirely, meaning it can be hard to spot that anything is wrong here. Linters do exist for this now, but they have so many false positives on other error handling cases that most people don't bother with them.

Re:Error handling in Go web apps shouldn't be so awkward by riwsky on Tuesday January 09, 2024 @09:25PM (#38935375)
From “the rise of worse is better”:

> Completeness -- the design must cover as many important situations as is practical. All reasonably expected cases should be covered. Completeness can be sacrificed in favor of any other quality. In fact, completeness must be sacrificed whenever implementation simplicity is jeopardized. Consistency can be sacrificed to achieve completeness if simplicity is retained; especially worthless is consistency of interface.

Re:Error handling in Go web apps shouldn't be so awkward by riwsky on Tuesday January 09, 2024 @09:23PM (#38935359)
Rust-the-language, maybe—but Rust-the-ecosystem makes heavy use of magic crates like `thiserror` and `anyhow`. I’m not saying it’s a better or worse philosophy, but it’s certainly a different philosophy.
Re:Error handling in Go web apps shouldn't be so awkward by kelnos on Wednesday January 10, 2024 @12:16AM (#38936502)
thiserror and anyhow exist for ergonomic purposes (and enhance the experience of using the question-mark operator). Libraries shouldn't be leaking thiserror or anyhow types into their public interface. If they are, that's bad design.
Re:Error handling in Go web apps shouldn't be so awkward by jeremyloy_wt on Tuesday January 09, 2024 @09:45PM (#38935494)
thiserror and anyhow are hardly magic.

The former is just macros for error definition

The latter is just a wrapper around box dyn error

Re:Error handling in Go web apps shouldn't be so awkward by riwsky on Wednesday January 10, 2024 @03:47PM (#38942858)
“Magic” isn’t a rigorously defined term, so let’s ignore it. Support for macros is a huge philosophical difference between Rust and Go.
Re:Error handling in Go web apps shouldn't be so awkward by jeremyloy_wt on Wednesday January 10, 2024 @05:07PM (#38944027)
I disagree in the context of error handling.

The go ecosystem frequently utilizes `go generate` to handle boilerplate code generation. That’s the exact same way `thiserror` utilizes macros to generate plain error structs. While macros can do more, in this case it’s the same.

Re:Error handling in Go web apps shouldn't be so awkward by randomdata on Tuesday January 09, 2024 @09:51PM (#38935545)
While sum types would be a welcome addition in general, summing an error is logically erroneous. It is an independent state.

The question mark is interesting, but as the try proposal discovered, how do you solve the leaking problem? That doesn't have a good solution yet.

Re:Error handling in Go web apps shouldn't be so awkward by valenterry on Tuesday January 09, 2024 @09:54PM (#38935566)
> Summing an error is logically erroneous. It is an independent state.

Doesn't compute for me. Care to explain?

Re:Error handling in Go web apps shouldn't be so awkward by wruza on Tuesday January 09, 2024 @10:24PM (#38935744)
In practice it’s either-or in “error handling”, because that’s when you decide what to do and not do next.
Re:Error handling in Go web apps shouldn't be so awkward by randomdata on Tuesday January 09, 2024 @10:34PM (#38935810)
Not true. As zero values are to be useful, one should never have to observe the error state unless the error is significant to the caller for some reason. Sometimes the error is significant. Sometimes it isn't. That depends on what the caller's needs are. Summing the error makes assumptions about the caller that may or may not be true. That is a bad API design.
Re:Error handling in Go web apps shouldn't be so awkward by mplanchard on Tuesday January 09, 2024 @10:58PM (#38935957)
Yes this is why you can just operate on the non error value (with map), immediately propagate the error (with ?), just operate on the error value (with map_err), or ignore it completely (eg by converting it to an option or using something like unwrap_or_else). Errors in Rust are still just possibly present values: they’re just wrapped in an interface (a sum type) that forces you to be explicit about your intent to ignore, handle, or propagate the error.
Re:Error handling in Go web apps shouldn't be so awkward by randomdata on Tuesday January 09, 2024 @11:02PM (#38935994)
Propagation suffers from the leaking problem. Rust has solutions for that, but it's not clear how that translates to Go, hence why the "try" proposal failed. I'm sure there is some solution out there, but nobody has come up with a good one. It turns out if nobody does the work, the work doesn't get done.

Again, needing to be explicit about your intent to do anything with an error is faulty. The values are not dependent. Just because some languages have gotten it wrong doesn't mean they all should.

Re:Error handling in Go web apps shouldn't be so awkward by valenterry on Tuesday January 09, 2024 @11:25PM (#38936145)
What is the "leaking problem"?
Re:Error handling in Go web apps shouldn't be so awkward by mplanchard on Wednesday January 10, 2024 @10:01AM (#38939601)
What you're saying doesn't make sense to me, and I also don't know what you're referring to by the leaking problem. I don't see how the choice of being explicit or non-explicit about errors is "faulty." Plenty of languages choose to use exceptions and maximize your ability to ignore errors, while others force you to deal with (in some way) the fact that an operation is fallible. Neither of these is "wrong," in my opinion, but they do offer different tradeoffs and ergonomics.

I'm not sure why you say that the values of a sum type must be "dependent," or what you mean by that. Maybe/Option is a sum type of Nothing or Some<T>. There's no dependency there. If my webserver can return JSON or XML, I might represent that with a sum type holding either a JSON object or an XML tree, but there's no dependency between those two things that I can see.

Re:Error handling in Go web apps shouldn't be so awkward by Thaxll on Tuesday January 09, 2024 @09:41PM (#38935478)
It's not atrocious, when you work on Go code on a daily basis it's fine but I agree that it's repetitive.

Really an overblown problem from people that don't use Go much tbh. I work with people that used extensivly Java / C# / C++ and it's not the thing they complain about. They usually enjoy the fact that it's not exception based.