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

runtime.Caller with custom frame skip count #15

Open
Raphy42 opened this issue Nov 16, 2022 · 0 comments
Open

runtime.Caller with custom frame skip count #15

Raphy42 opened this issue Nov 16, 2022 · 0 comments

Comments

@Raphy42
Copy link

Raphy42 commented Nov 16, 2022

There is a common pattern in domain logic, where the creation of errors has been standardised with helper functions.
For example in a http handler one might want to easily create a validation error, with a custom errorCode, without having to type the complete stacktrace boilerplate.

if err := domain.DoTheThing(ctx, args); err != nil {
   // call to stacktrace.Propagate has been deferred to an opaque package
   // containing all of the domain semantic conventions
   return nil, errors.InvalidThingDone(err, args)
}
// stacktrace will always point to its caller, which is in this case the helper function

The problem is that stacktrace will use the helper function callsite instead of the line of invocation of said helper function.
It would not be a problem if go allowed explicit inlining, but that does not seem possible currently.

The idea would be to add an optional parameter in order to skip n runtime call frames, allowing forward declaration of error helpers, without impacting the stacktrace.
Is it something that could benefit this library (as a new public function), or is it something totally out of scope for the community ?

// example implementation of the `errors` package with proposed functionnalities
// API is given as an example

// builder created at runtime
var invalidThingDoneErr = stacktrace.NewErrorBuilder()

// invokes the error builder which skips this frame, being the actual helper call site
func InvalidThingDone(err error, args any) error {
   return invalidThingDoneErr.
      WithCode(0xdeadbeef).
      Propagate(err)
} 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant