func (t *Thing) Process(id int) (string, error) {
return "", fmt.Errorf("not implemented")
}
and then filling them in gradually like func (t *Thing) Process(id int) (string, error) {
dat, err := t.store.Read(id)
if err != nil {
return "", fmt.Errorf("error reading ID: %w", err)
}
cert, err := dat.ExtractCertificate()
if err != nil {
return "", fmt.Errorf("error extracting certificate: %w", err)
}
return cert.Name(), nil
}
and explicitly not func (t *Thing) Process(id int) (string, error) {
dat, _ := t.store.Read(id) // TODO: error handling
cert, _ := dat.ExtractCertificate() // TODO: error handling
return cert.Name(), nil
}
Writing code this way, explicit error handling upfront, is fundamental to reliability (for a large class of applications).Even worse, with this style of programming, someone up the stack who would actually want to handle these errors has no mechanism to do, since you're returning the same type from both error cases. If they wanted to handle the certificate error but not the read error, they would have to parse the error message string, which is brittle. But if you did want to add appropriate context, your function would bloat even more. Not to mention that the standard library doesn't really help, since it generally doesn't define any specific error types to set up some patterns for this. Even in your example, from the start you assumed that your function can either succeed or fail in a generic way, you didn't think that the signature may want to encode multiple different error types, which is what GP was talking about when saying you can't usually think about the sad case before the happy case. Sure, if the extent to which you want to define sad case first is 'sad case can happen', you can.
Go's error handling strategy is its weakest aspect, and it is annoying to hear advocates pretend that Go is doing it right, when 90% of Go code is doing the same thing as 90% of exception-based code, just manually and with less helpful context for either logging or for the 10% of code which actually wants to handle errors.
You look at what I'm doing as a more tedious and error-prone version of exception bubbling, but that misses the forest for the trees. The whole point of doing it this way is to lift errors out of the shadows of the exception control flow path, and put them front-and-center in the actual logic of the application. Programming (in many domains) is error handling, the error handling is at least and arguably more important than the business logic.
I don't want exceptions. I do want this (or something like it).
> Even worse, with this style of programming, someone up the stack who would actually want to handle these errors has no mechanism to do, since you're returning the same type from both error cases.
As the author of this module, I get to decide what my callers are able to see. What I've written is (IMO) the most straightforward and best general-purpose example, where callers can still test for the wrapped errors if they need to. If it were important for callers to distinguish between Read and Certificate errors, I would use sentinel errors e.g. var ErrCert (if the details weren't important) or custom error types e.g. type CertificateError struct (if they were).
Adding this stuff isn't bloat. Again, it's just as important as the business code itself.
> Go's error handling strategy is its weakest aspect, and it is annoying to hear advocates pretend that Go is doing it right
In industry, considering languages an organization can conceivably hire for, and considering the general level of industry programmers -- programs written in Go are consistently in the top tier for reliability, i.e. fewest bugs in logic, and fewest crashes. Certainly more reliable than languages with similar productivity like Python, Ruby, Node, etc.
There are plenty of flaws in Go's approach to error handling -- I would love to have Option or Result types, for example -- but I think, judging by outcomes, it's pretty clear that Go is definitely doing something right.