News for hackers. Stuff that matters.
Re:Go Developer Survey 2022 Q2 Results by klodolph on Thursday September 08, 2022 @12:43PM (#32768131)
I would love to see some in-depth discussion about this. Even in 2022, I don't think it's clear what we want error handling to look like. You can ask for correctness guarantees from the compiler, but it's easy to ask, it's harder to design a system that actually works.

> The lack of exhaustive switch case statements makes this worse because the compiler doesn't care if you're handling all the expected error types (or unexpected ones).

I would love to see how this is supposed to work.

My first thought here is checked exceptions in Java. With checked exceptions, you have a specific set of exceptions that a function is supposed to generate, and a compile-time error if those exceptions are not either handled or propagated. This turned out to be a bit of a disaster in actual practice, and I'm not sure what the fix would look like here.

I'm generally a bit miserable when I try to deal with error handling in Rust, believe it or not.

As for the err != nil boilerplate, I've seen some proposals but I haven't seen anything that was clearly a good proposal. The generics proposal was clearly good, IMO, just for a point of reference.

Re:Go Developer Survey 2022 Q2 Results by indemnity on Thursday September 08, 2022 @01:32PM (#32768831)
> I'm generally a bit miserable when I try to deal with error handling in Rust, believe it or not.

Likewise. I'm a big Rust user, use it for all my personal projects, but error handling is totally unsolved there.

- Friction around mapping between error types if I need to "add context and rethrow"

- Often lack of stack/trace information for custom error types. Something went wrong the the DB query? Great! Where? Which line of code?

There are solutions for these, I try them, and I'm sure someone will pipe up mentioning them (e.g. anyhow, thiserror, miette, etc), but its just so much damn ceremony to do it properly at a particular call site, especially when you are mixing your own code with library code and each library's unique snowflake error types, that I don't do it consistently.

Re:Go Developer Survey 2022 Q2 Results by gregwebs on Thursday September 08, 2022 @01:32PM (#32768818)
In Rust you can use the anyhow library if you want a similar level of overhead to Go and similarly aren’t worried about differentiating error cases.

What do you think of adopting Zig’s error handling approaches to Go?

Re:Go Developer Survey 2022 Q2 Results by klodolph on Thursday September 08, 2022 @03:03PM (#32770421)
> [...] and similarly aren’t worried about differentiating error cases.

I differentiate error cases in Go, so I'm not sure what you're talking about. Sometimes it's done with ==, other times it's done with type assertions, type switches, and some helper functions in the errors package.

I've barely touched Zig.

I think it's contributing some important ideas, and maybe those ideas will get further developed in newer languages.

Re:Go Developer Survey 2022 Q2 Results by treis on Thursday September 08, 2022 @02:09PM (#32769476)
I'd like stuff like

myFunc() works as today

myFunc()^ returns the err if there is one

myFunc()! panics if there is an err

Re:Go Developer Survey 2022 Q2 Results by majewsky on Thursday September 08, 2022 @06:43PM (#32773022)
> myFunc()! panics if there is an err

You can do this with generics today:

  func must[T](val T, err error) T {
    if err != nil {
      panic(err.Error())
    }
    return val
  }

  //usage example
  file := must(os.Create("out.txt"))
  defer file.Close()
  bytesWritten := must(file.Write([]byte("Hello World\n")))
We have something like this in our internal utility library. I'm getting some serious mileage out of this when using Go as a script language, where error propagation is not as important as for a regular application or a library.

> myFunc()^ returns the err if there is one

You can also do this with generics:

  func geterr[T](val T, err error) {
    return err
  }
It is true that must() and geterr() are slightly more verbose than single-character operators, but I'd argue having a word instead of a symbol is much more readable. Besides, ^ cannot be used as a postfix unary operator because it's already a binary operator. That would create a grammar ambiguity.
Re:Go Developer Survey 2022 Q2 Results by klodolph on Thursday September 08, 2022 @02:52PM (#32770259)
I don't think any option should panic on error. I've had to deal with programs that panic on error and the way I dealt with it was by forking the project and removing every call to panic(). It's even worse with libraries.

The other problem is that it's super common to want to wrap the error somehow when returning it. Often you want to wrap the error differently depending on where the error came from in the function, like this:

  d, err := os.ReadFile(fname)
  if err != nil {
    return err // unwrapped
  }
  x, err := parseX(d)
  if err != nil {
    return fmt.Errorf("%s: %w", fname)
  }
I know that not all Go developers write good error handling code. However, I don't want to make it easier to write bad error handling code if you're not making it correspondingly easier to write good error handling code.
Re:Go Developer Survey 2022 Q2 Results by jen20 on Saturday September 10, 2022 @07:19AM (#32789625)
Aborting is almost always the correct answer in order that you get a core dump that may be debugged post-mortem. Blindly continuing when in an invalid state is a horrible idea.
Re:Go Developer Survey 2022 Q2 Results by metaltyphoon on Thursday September 08, 2022 @04:02PM (#32771256)
Unless you put the function name somewhere in the error, there is no way to know where something happened. "Some thing didn't write properly? Cool! Where and which line?"
Re:Go Developer Survey 2022 Q2 Results by klodolph on Friday September 09, 2022 @01:48PM (#32782550)
> "Some thing didn't write properly? Cool! Where and which line?"

My eyes glaze over when I'm trying to read stack traces in Java or Python, and then it turns out I can't figure out what's wrong because the stack trace doesn't include the path of the file that failed to parse.

What we care about is stuff like:

- Program tried to parse file X,

- Failed because file X doesn't exist, or because file contains syntax errors, or something

- Some context giving us the reason why program tried to read file X, which is usually at a fairly high level (something like "could not load config").

Putting the function name in the error-handling code is something that you SOMETIMES do, but only when it adds important context. If you do it all the time you end up with something completely unreadable, like a stack trace. There are various libraries you can use that give you stack traces for errors in Go, if that's what you want.

If I get an error that X is not a valid JSON file--often, that's all I need to know, because I can open X and see the syntax error and fix it. Knowing the name of the function that detected the syntax error is unhelpful, usually, and we have logging + debuggers to help with those cases.

Re:Go Developer Survey 2022 Q2 Results by lenkite on Thursday September 15, 2022 @03:53PM (#32857632)
Java stack-traces are extraordinarily informative - they give line number, class, method and even offset inside relative lambda when an exception occurs. Few languages do better and Go is not one of them.
Re:Go Developer Survey 2022 Q2 Results by dannymi on Friday September 09, 2022 @06:57AM (#32777720)
If it's an internal (unexpected) error, panic. You get a backtrace and everything--which helps because it was an unexpected internal error, so you probably messed up. You'd like to know where that is.

But if it's an actual user-visible error code then that means you expected for this error to happen eventually and you designed for it. Why then do you need a backtrace? You program is doing what it's supposed to.

I mean sure while developing it's nice to be able to just replace the error code emission by panic (which I do sometimes)--but in regular operation? It doesn't help the user at all.

Re:Go Developer Survey 2022 Q2 Results by thiht on Friday September 09, 2022 @05:58AM (#32777361)
> I've seen some proposals but I haven't seen anything that was clearly a good proposal

I have one! Allowing inline ifs, like this:

    err := func()
    if err != nil { return err }
1 line instead of 3 for simple error handling, yeaah