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:59:01 -07:00
parent fe49018479
commit f44e43174c

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,
}
}