Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: runtime: provide a way to format output in unhandled panics #63455

Open
mitar opened this issue Oct 9, 2023 · 0 comments
Open

proposal: runtime: provide a way to format output in unhandled panics #63455

mitar opened this issue Oct 9, 2023 · 0 comments
Labels
error-handling Language & library change proposals that are about error handling. Proposal
Milestone

Comments

@mitar
Copy link
Contributor

mitar commented Oct 9, 2023

Background

When code calls panic(err) and panic is not handled (recovered) it aborts the whole program while printing the panic value and goroutine stack traces. If the panic value implements the error interface, code currently calls its Error method and prints it out.

Proposal

I propose that the code there first checks for the following interface:

type panicError interface {
    error
    PanicError() string
}

And if the value implements this interface, PanicError is called instead of Error to obtain the string to print out.

Discussion

I am the author of the gitlab.com/tozd/go/errors, a modern drop-in replacement for github.com/pkg/errors which enables errors to be annotated with stack traces and structured details. I noticed that it is pretty common for people to pass error values to panic. But the issue is that errors with additional optional information do not have any way to output that information when unhanded panic occurs. For example, errors contain a stack trace where they happened but that stack trace is not printed out, nor is available among stack traces printed out by runtime itself (the error might have happened at a completely different place than where panic is called). Errors do print that additional information when using fmt.Printf, but that is not used by the runtime.

So I suggest that runtime checks for the above interface first, and if it exists, calls it, giving errors an option to provide additional useful information for debugging. The cost of doing this should be minimal (unhandled panics are rare, it is one extra type case in the existing type switch).

Alternatives

Errors could add all extra information to output from Error() but generally none of libraries I have seen is doing that. The reason is probably simple: such strings with extra information are hard to combine while Error() strings generally stay oneliners (except when using errors.Join).

@mitar mitar added the Proposal label Oct 9, 2023
@gopherbot gopherbot added this to the Proposal milestone Oct 9, 2023
@seankhliao seankhliao added the error-handling Language & library change proposals that are about error handling. label Oct 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error-handling Language & library change proposals that are about error handling. Proposal
Projects
Status: Incoming
Development

No branches or pull requests

3 participants