renamed CallTrace to CallStack and implemented Error.Stack() method

This commit is contained in:
milarin
2023-07-27 19:20:02 +02:00
parent ad6d42b35c
commit 1bc43397d6
3 changed files with 17 additions and 13 deletions

View File

@ -9,7 +9,7 @@ import (
// Error is a wrapper for error with stack trace
type Error struct {
msg string
callTrace *CallTrace
callStack *CallStack
tmpl *ErrTmpl
cause error
prev []error
@ -19,7 +19,7 @@ type Error struct {
func New(msg string) *Error {
return &Error{
msg: msg,
callTrace: Trace(2),
callStack: Trace(2),
}
}
@ -28,7 +28,7 @@ func Wrap(msg string, cause error) *Error {
return &Error{
msg: msg,
cause: cause,
callTrace: Trace(2),
callStack: Trace(2),
}
}
@ -37,7 +37,7 @@ func Wrap(msg string, cause error) *Error {
func Chain(msg string, errors []error) *Error {
return &Error{
msg: msg,
callTrace: Trace(2),
callStack: Trace(2),
prev: errors,
}
}
@ -70,6 +70,10 @@ func (e *Error) Message() string {
return e.msg
}
func (e *Error) Stack() *CallStack {
return e.callStack
}
// Is implements the error equality function used by errors.Is()
// It returns true if the error is the same instance or is created using the same ErrTmpl
func (e *Error) Is(target error) bool {
@ -137,7 +141,7 @@ func printErr(err error, b *strings.Builder) {
b.WriteString(": ")
b.WriteString(e.msg)
b.WriteString("\n")
b.WriteString(e.callTrace.String())
b.WriteString(e.callStack.String())
} else {
b.WriteString(errtype(err))
b.WriteString(": ")