Hacker News new | ask | show | jobs
by orangeduck 4401 days ago
So I get it, and it makes a good point, but this could be more of a symptom than a disease.

When I first started programming C I also tended to just exit the program on getting a bad return code, because I'd never been taught how to properly handle errors - I'd always just replied on exceptions.

Only later did I start to get bitten, and take proper care to handle errors sensibly and explicitly.

As "go" is such a new language I wouldn't be surprised if a similar thing has happened to new programmers migrating over to it.

1 comments

I find that I wind up "bubbling up" errors of my own to build a stack trace yet still retain control of my program, like:

  func LowLevelCode() (int, error) {
    // rest of body...
    if /* error condition */ {
      return 0, errors.New("LowLevelCode error: " + /* error description */)
    }
  }

  func MiddleWare() (interface{}, error) {
    // body of code...
    nCount, err := LowLevelCode()
    if err != nil {
      return nil, errors.New("MiddleWare error: " + err.Error())
    }
  }

  func ApplicationCode() {
    // Do cool stuff...
    obj, err := MiddleWare()
    if err != nil {
      log.Println(err) // Prints "MiddleWare error: LowLevelCode error: " + ...
      // Handle on a high level, retry connections, etc.
    }
  }
I'm hoping someone comes in and corrects me with a more elegant way of "building up your own stack trace" without having to do this manually and without panic.
There are a lot of error handling libraries out there. On Juju, we're working on one right now. This is the preliminary version: https://github.com/juju/errgo

Basically you just call a function every time you would return an error, and it automatically records the file and line number and lets you add on some context to the message, plus it gives you the option to mask the type of the error (to hide implementation details from your callers).

I wouldn't use errgo right now, it's still under heavy development, but it's similar to a lot of other error handling libraries out there.