diff --git a/pkg.go b/pkg.go index 562452a..9d821e2 100644 --- a/pkg.go +++ b/pkg.go @@ -4,6 +4,7 @@ import ( // Standard "fmt" "path" + "regexp" "runtime" ) @@ -87,10 +88,6 @@ func Wrap(err error) *Error { return create(err, nil) } -func WrapData(err error, data interface{}) *Error { - return create(err, data) -} - // New creates a new wrapped error with file and line. func New(msg string, params ...any) *Error { err := fmt.Errorf(msg, params...) @@ -98,24 +95,31 @@ func New(msg string, params ...any) *Error { return wrapped } -// NewData creates a new WrappedError with associated data. -func NewData(msg string, data interface{}, params ...any) *Error { - err := fmt.Errorf(msg, params...) - wrapped := create(err, data) - return wrapped -} - +// WithCode associates a string code with the error. func (e *Error) WithCode(code string) CodableError { e.Code = code return e } +// WithData associates any data with the error to make troubleshooting easier. func (e *Error) WithData(data any) CodableError { e.Data = data return e } +// Log calls the log callback. func (e *Error) Log() CodableError { callback(*e) return e } + +// NoTrace returns the Error() string without the trace. +func (e *Error) NoTrace() string { + rxp := regexp.MustCompile(`^(\[[^\]]+\.go:\d+\]\s*)*`) + return string( + rxp.ReplaceAll( + []byte(e.Error()), + []byte(""), + ), + ) +}