-
Notifications
You must be signed in to change notification settings - Fork 39
Description
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 functionThe 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)
}