added wrapper error that will provide file/line origin of an error
Some checks failed
Go / build (1.19) (push) Failing after 13s

This commit is contained in:
Colin Henry 2024-09-29 11:57:03 -07:00
parent fe49018479
commit 85eeb0f7eb

38
pkg.go
View File

@ -2,6 +2,8 @@ package x
import (
"errors"
"fmt"
"runtime"
)
func Assert(cond bool, msg string) {
@ -37,3 +39,39 @@ func (i *invariants) First() error {
func (i *invariants) All() []error {
return i.errs
}
type xError struct {
LineNo int
File string
E error
Debug bool
}
func (e *xError) Error() string {
if e.Debug {
return fmt.Sprintf(
"%s\n\t%s:%d", e.E, e.File, e.LineNo,
)
}
return e.E.Error()
}
func (e *xError) Unwrap() error {
return e.E
}
func NewError(unwrapped error, debug bool) error {
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "unknown"
line = 0
}
return &xError{
LineNo: line,
File: file,
E: unwrapped,
Debug: debug, // Populate from the global flag
}
}