News for hackers. Stuff that matters.
Re:Upcoming Features in Go 1.18 by PaulKeeble on Tuesday November 09, 2021 @11:43AM (#29163976)
We need a shortcut for the

if x,err = f(); err!=nil { return _,err }

pattern that is so common in go code. This is my biggest daily gripe and I work around it with a snippet but so much of integration code is this or a variant of it which captures the stack trace and it constantly distracts from the main flow of algorithm.

Re:Upcoming Features in Go 1.18 by dullgiulio on Tuesday November 09, 2021 @12:11PM (#29164338)
Please no. I am still fighting the battle of adding contextual information to the error using fmt.Errorf.

I am tired of seeing

SocketException: hostname.com

as full error message.

Re:Upcoming Features in Go 1.18 by AYBABTME on Tuesday November 09, 2021 @12:12PM (#29164354)
This, errors that are bubbled up should have some context added to them. Otherwise every error ends up being totally meaningless.
Re:Upcoming Features in Go 1.18 by stouset on Tuesday November 09, 2021 @12:39PM (#29164700)
I repeatedly fail to understand why golang proponents insist on flogging themselves into becoming human stacktrace authors. This is a solved problem the computer can do better than you, automatically, on your behalf.

Certainly any shortcut syntax should not prevent you from adding human-curated context to your errors. But the current situation comes at an enormous cost, and I truly think there's a bit of Stockholm Syndrome going on here.

Re:Upcoming Features in Go 1.18 by feffe on Tuesday November 09, 2021 @01:41PM (#29165568)
It's not the same thing. In Java and Python programs, especially Java programs, developers seems to think it's perfectly reasonable to present the end user with a 100 entries stack trace. This may be of use to the developer to debug issues but it's not a substitute for good error messages that are to be presented to the end user.

I don't think Go errors are perfect either as you just tend to concatenate strings together but the information they include are more user friendly if done right, i.e. not just bubble up the error, but adding context that the user is aware of.

Re:Upcoming Features in Go 1.18 by pg_1234 on Tuesday November 09, 2021 @02:31PM (#29166308)
Languages that bubble errors up still let you catch and categorize the errors at the top, allowing for very presentable formats for the end user ... in fact, in my experience, because they don't have to deal with tiers of pointless boilerplate, devs put the effort instead into better end user/public errors.

I say this as an ex-(for now)-Python dev, currently suffering though Go's tedious error handling.

Re:Upcoming Features in Go 1.18 by feffe on Tuesday November 09, 2021 @03:00PM (#29166700)
I agree that in theory the same is possible in languages that use exceptions but in my experience laziness often wins out. The exception is caught on the level where it's suitable to terminate the stack unwinding and deal with the error from the applications point of view. At that point it's too late to add user recognizable context to the error that what Go advocates by its design.

My opinion is that a syntax shortcut such as "?" to just bubble up errors without adding context would still be useful. I would welcome it. There are cases where I find that no additional context is needed. I think the Go designers are afraid what it would lead to though (the laziness wins out hypothesis)

Re:Upcoming Features in Go 1.18 by OldHand2018 on Wednesday November 10, 2021 @09:24AM (#29174918)
> especially Java programs, developers seems to think it's perfectly reasonable to present the end user with a 100 entries stack trace

Try debugging something in Spark/DataBricks. It is so incredibly tedious; stack traces can easily reach 200 lines or more.

Re:Upcoming Features in Go 1.18 by kevinmgranger on Tuesday November 09, 2021 @01:30PM (#29165423)
Not that I agree with how Go handles it, but the computer is not always better at it. It's a tradeoff, a compromise, or a catch-22.

If you use the full stacktrace, you could get 50 frames of irrelevant library code worsening the signal:noise ratio, hiding what the real issue is.

If you write your own context, then when it _is_ a problem with library code, you're hopelessly lost.

I think we need better tooling: the ability to add your own contextual information, to propagate errors explicitly but without boilerplate, and the ability to _choose_ the level of information you see afterward.

We have log viewers that can filter out by severity level, why don't we have a standardization for stack traces to let you filter to what you want?

Re:Upcoming Features in Go 1.18 by geodel on Tuesday November 09, 2021 @01:45PM (#29165642)
> I truly think there's a bit of Stockholm Syndrome going on here.

I agree in sense Stockholm Syndrome going on but not just for Go but with almost every technology including Go.

When we have folks defending Rust slow builds, Swift breaking changes, Half-assed tooling for Scala, poor power management on linux laptops and on and on. It could mean either plain old stockholm syndrome or that people can work around these minor irritants and get main benefit of technology. Just like with anything else in life.

Re:Upcoming Features in Go 1.18 by AYBABTME on Tuesday November 09, 2021 @02:29PM (#29166269)
Attributing this to stockholm syndrome is a bit convenient and reductionist.

Stacktraces are better than just bubbling up, and yes a mechanism to minimize if-err could also allow for human annotations.

I'd love if Go had a stronger type system, but I know it'd probably come with longer compile times like Rust has. I think it's not a dogmatic take, and I think a lot of golang proponent do take the same nuanced position.

Re:Upcoming Features in Go 1.18 by jatone on Tuesday November 09, 2021 @02:41PM (#29166442)
just use pkg/errors? it includes the stack trace. there wasn't a need to include this in the stdlib thats all.
Re:Upcoming Features in Go 1.18 by epage on Tuesday November 09, 2021 @01:16PM (#29165220)
It can be done well, see Zig's traces [0].

[0] https://ziglang.org/documentation/master/#Error-Return-Trace...

Re:Upcoming Features in Go 1.18 by ladberg on Tuesday November 09, 2021 @02:24PM (#29166209)
I don't see how those are mutually exclusive. You could do something like:

  x, err := f(); if err != nil return fmt.Errorf("More context -> %s", err)
(I'm not attached to that specific syntax, it's just an example of a hypothetical one-liner)
Re:Upcoming Features in Go 1.18 by sethammons on Tuesday November 09, 2021 @02:45PM (#29166495)
Doing some analysis, "naked" if-err-return-err shows up in about 5% of error handling cases in my team's most critical service.

The rest of the error blocks do something more (attempt a fallback, add context, log something, metric something, etc). This is code base that has been worked on for about 6 years, around 40k lines of code, 32 contributors, and over 450 releases. This is a legit production mail transfer agent. Take this as an experience report on a real system: that pattern is not as common as you think. 5% is pretty low.

Re:Upcoming Features in Go 1.18 by irq-1 on Tuesday November 09, 2021 @03:34PM (#29167093)
Curious if there is a one liner that would cover 80%+ of the error handling: onerror add context and return ; onerror function(err) and return ;
Re:Upcoming Features in Go 1.18 by sethammons on Tuesday November 09, 2021 @03:56PM (#29167370)
Error handling differs. But we try to move up metrics and logs as high as possible in the call chain. Handling an error may be a retry with a strategy, moving items to different queues, changing concurrency, potentially triggering jobs to run, feeding different events to event streams, and, of course, retuning errors to callers so they can programmatically respond.
Re:Upcoming Features in Go 1.18 by silisili on Tuesday November 09, 2021 @12:13PM (#29164363)
What shortcuts would you recommend?

The pattern itself as written above is rather useless. What you end up with is a call 3 libraries deep that returns nothing but "EOF", and you have no idea what happened. In practice, you should really wrap every error return in a message and/or with a callstack. If the shortcut method can a) accomplish that, and b) avoid everything being nested, like try/except tends to do, I think I'd be more interested in the concept.

Re:Upcoming Features in Go 1.18 by lalaithion on Tuesday November 09, 2021 @12:29PM (#29164567)
I don't actually mind writing the above and adding context, but let's brainstorm some syntax that does the same thing without being verbose. How about something like:

    $variable1 := try $function_call_which_returns_two_values except $variable2: $expression_which_has_error_type
which is automatically desugared to

    $variable1, $variable2 := $function_call_which_returns_two_values
    if $variable2 != nil {
       return ($zero_values_for_other_return_types, )* $expression_which_has_error_type
    }
Re:Upcoming Features in Go 1.18 by mseepgood on Tuesday November 09, 2021 @12:49PM (#29164855)
> let's brainstorm some syntax

Don't forget to check if someone else already had the same idea: https://seankhliao.com/blog/12020-11-23-go-error-handling-pr...

Re:Upcoming Features in Go 1.18 by morelisp on Tuesday November 09, 2021 @12:31PM (#29164592)
Re:Upcoming Features in Go 1.18 by lalaithion on Tuesday November 09, 2021 @12:44PM (#29164782)
The try syntax proposed there and in other places returns the error unwrapped, which is a no-go for me. I wrap every error that I return from every function.
Re:Upcoming Features in Go 1.18 by encoderer on Tuesday November 09, 2021 @01:42PM (#29165593)
Oh can I see how you do it?
Re:Upcoming Features in Go 1.18 by camkego on Wednesday November 10, 2021 @05:03AM (#29172995)
Please share articles/blog posts! I would guess there are newer articles about best practices for Go error handling, other than the great articles by Dave Cheney. https://dave.cheney.net/2016/04/27/dont-just-check-errors-ha...
Re:Upcoming Features in Go 1.18 by morelisp on Tuesday November 09, 2021 @12:21PM (#29164452)
Please no allocations hidden in the short syntax. I use `errors.WithMessage` / `fmt.Errorf` with `%w` often within my application's main execution flow, but I don't need a stack trace attached to every missing file, parse failure, or connection reset.
Re:Upcoming Features in Go 1.18 by silisili on Tuesday November 09, 2021 @12:34PM (#29164625)
Apologies, to be more precise, my team wraps every error in a filename/line number pair, not an entire stacktrace.
Re:Upcoming Features in Go 1.18 by morelisp on Tuesday November 09, 2021 @01:00PM (#29165008)
What is the (moral or performance) difference between wrapping every error you return anywhere with a filename/lineno pair, and a stack trace? If anything, the stacktrace proper probably only needs to allocate once, at the lowest level.
Re:Upcoming Features in Go 1.18 by silisili on Tuesday November 09, 2021 @01:14PM (#29165184)
The specifics of it are deep enough in runtime that I've never actually looked, but it's designed as a chain to return a specific caller - it looks like -

    _, file, line, _ := runtime.Caller(2)
    locTxt := fmt.Sprintf(" (at %s:%d)", filepath.Base(file), line)
I think if the above on errors became a performance concern, I'd heavily rethink what I considered an error. But, it is also possible the systems we've designed are miles apart in function and philosophy.
Re:Upcoming Features in Go 1.18 by morelisp on Tuesday November 09, 2021 @01:21PM (#29165291)
Yeah, this is fine for like "unable to contact database server" or "kafka producer timeout" where you're gonna terminate a large portion (or all) of the application at a much higher, removed level. "Lifecycle" stuff.

But if you make the short syntax do something like this, it's gonna get used for hundreds of library's equivalents of `Atoi` or `ParseQuery`. I don't need line annotations on my `http.ResponseWriter#Write` timeouts, they're slow enough already.

(And if you are going to do it - really, just attach the whole stack trace then and there, it will probably be faster.)

Re:Upcoming Features in Go 1.18 by mariusor on Wednesday November 10, 2021 @03:00AM (#29172213)
I do this based on build tags for dev vs. production environments. In dev I attach the stacktrace to my errors, in prod I do not.
Re:Upcoming Features in Go 1.18 by dullgiulio on Tuesday November 09, 2021 @12:25PM (#29164512)
No shortcuts.

Errors reach end users, and they cannot read stack traces.

Every shortcut makes for bad errors, bad troubleshooting and unhappy users.

Errors document code (you literally write, in English, what you were trying to do but failed, at each level of your program.)

Errors report to the user the clear intent that failed and why.

Errors are a huge differentiator in quality. There is no shortcut to quality.

Re:Upcoming Features in Go 1.18 by cy_hauser on Tuesday November 09, 2021 @02:31PM (#29166306)
> Errors reach end users, and they cannot read stack traces.

In general, users don't care about errors or stack traces. They don't want errors of any kind. As a developer I want users reporting errors, which can include a dense stack trace, so I can get the error and an explanation of what they were doing when the error happened. I'm more for making my life easier than worrying about what a user sees.

Re:Upcoming Features in Go 1.18 by 0des on Tuesday November 09, 2021 @12:51PM (#29164885)
Absolutely not.