perf(error): better deal with deferred errors

This commit is contained in:
Antoine Aflalo
2024-09-09 14:28:24 -04:00
parent a80997835a
commit 5b183cca29
4 changed files with 130 additions and 52 deletions

View File

@@ -0,0 +1,16 @@
package errs
import (
"errors"
"fmt"
)
// Capture runs errFunc and assigns the error, if any, to *errPtr. Preserves the
// original error by wrapping with errors.Join if the errFunc err is non-nil.
func Capture(errPtr *error, errFunc func() error, msg string) {
err := errFunc()
if err == nil {
return
}
*errPtr = errors.Join(*errPtr, fmt.Errorf("%s: %w", msg, err))
}