From 1bc43397d6dbe13f6f5312fefc76cceb0b807c73 Mon Sep 17 00:00:00 2001 From: milarin Date: Thu, 27 Jul 2023 19:20:02 +0200 Subject: [PATCH] renamed CallTrace to CallStack and implemented Error.Stack() method --- calltrace.go | 10 +++++----- error.go | 14 +++++++++----- error_tmpl.go | 6 +++--- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/calltrace.go b/calltrace.go index 1479173..f49f284 100644 --- a/calltrace.go +++ b/calltrace.go @@ -6,15 +6,15 @@ import ( "strings" ) -// CallTrace represents a call stack trace similar to Java's stack trace -type CallTrace struct { +// CallStack represents a call stack trace similar to Java's stack trace +type CallStack struct { frames *runtime.Frames more bool } // Trace returns a new CallTrace starting from this call // Use skip to skip the first entries in the trace -func Trace(skip int) *CallTrace { +func Trace(skip int) *CallStack { if DisableTrace { return nil } @@ -22,10 +22,10 @@ func Trace(skip int) *CallTrace { pc := make([]uintptr, CallStackLength+1) n := runtime.Callers(skip+1, pc) pc = pc[:n] - return &CallTrace{runtime.CallersFrames(pc), n == CallStackLength+1} + return &CallStack{runtime.CallersFrames(pc), n == CallStackLength+1} } -func (ct *CallTrace) String() string { +func (ct *CallStack) String() string { if ct == nil { return "" } diff --git a/error.go b/error.go index 5915b41..1da7bef 100644 --- a/error.go +++ b/error.go @@ -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(": ") diff --git a/error_tmpl.go b/error_tmpl.go index 1d51860..249958a 100644 --- a/error_tmpl.go +++ b/error_tmpl.go @@ -30,7 +30,7 @@ func (t *ErrTmpl) New(args ...interface{}) *Error { msg: fmt.Sprintf(t.format, args...), cause: nil, tmpl: t, - callTrace: Trace(2), + callStack: Trace(2), } } @@ -40,7 +40,7 @@ func (t *ErrTmpl) Wrap(cause error, args ...interface{}) *Error { msg: fmt.Sprintf(t.format, args...), cause: cause, tmpl: t, - callTrace: Trace(2), + callStack: Trace(2), } } @@ -49,7 +49,7 @@ func (t *ErrTmpl) Wrap(cause error, args ...interface{}) *Error { func (t *ErrTmpl) Chain(msg string, errors []error) *Error { return &Error{ msg: msg, - callTrace: Trace(2), + callStack: Trace(2), tmpl: t, prev: errors, }