Added NoTrace function and removed WrapData, NewData since WithData exist.

This commit is contained in:
Magnus Åhall 2024-09-27 07:42:30 +02:00
parent 1154fd39a2
commit 616835f641

26
pkg.go
View File

@ -4,6 +4,7 @@ import (
// Standard // Standard
"fmt" "fmt"
"path" "path"
"regexp"
"runtime" "runtime"
) )
@ -87,10 +88,6 @@ func Wrap(err error) *Error {
return create(err, nil) 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. // New creates a new wrapped error with file and line.
func New(msg string, params ...any) *Error { func New(msg string, params ...any) *Error {
err := fmt.Errorf(msg, params...) err := fmt.Errorf(msg, params...)
@ -98,24 +95,31 @@ func New(msg string, params ...any) *Error {
return wrapped return wrapped
} }
// NewData creates a new WrappedError with associated data. // WithCode associates a string code with the error.
func NewData(msg string, data interface{}, params ...any) *Error {
err := fmt.Errorf(msg, params...)
wrapped := create(err, data)
return wrapped
}
func (e *Error) WithCode(code string) CodableError { func (e *Error) WithCode(code string) CodableError {
e.Code = code e.Code = code
return e return e
} }
// WithData associates any data with the error to make troubleshooting easier.
func (e *Error) WithData(data any) CodableError { func (e *Error) WithData(data any) CodableError {
e.Data = data e.Data = data
return e return e
} }
// Log calls the log callback.
func (e *Error) Log() CodableError { func (e *Error) Log() CodableError {
callback(*e) callback(*e)
return 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(""),
),
)
}