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

@ -6,15 +6,15 @@ import (
"strings" "strings"
) )
// CallTrace represents a call stack trace similar to Java's stack trace // CallStack represents a call stack trace similar to Java's stack trace
type CallTrace struct { type CallStack struct {
frames *runtime.Frames frames *runtime.Frames
more bool more bool
} }
// Trace returns a new CallTrace starting from this call // Trace returns a new CallTrace starting from this call
// Use skip to skip the first entries in the trace // Use skip to skip the first entries in the trace
func Trace(skip int) *CallTrace { func Trace(skip int) *CallStack {
if DisableTrace { if DisableTrace {
return nil return nil
} }
@ -22,10 +22,10 @@ func Trace(skip int) *CallTrace {
pc := make([]uintptr, CallStackLength+1) pc := make([]uintptr, CallStackLength+1)
n := runtime.Callers(skip+1, pc) n := runtime.Callers(skip+1, pc)
pc = pc[:n] 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 { if ct == nil {
return "" return ""
} }

View File

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

View File

@ -30,7 +30,7 @@ func (t *ErrTmpl) New(args ...interface{}) *Error {
msg: fmt.Sprintf(t.format, args...), msg: fmt.Sprintf(t.format, args...),
cause: nil, cause: nil,
tmpl: t, 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...), msg: fmt.Sprintf(t.format, args...),
cause: cause, cause: cause,
tmpl: t, 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 { func (t *ErrTmpl) Chain(msg string, errors []error) *Error {
return &Error{ return &Error{
msg: msg, msg: msg,
callTrace: Trace(2), callStack: Trace(2),
tmpl: t, tmpl: t,
prev: errors, prev: errors,
} }