renamed CallTrace to CallStack and implemented Error.Stack() method
This commit is contained in:
parent
ad6d42b35c
commit
1bc43397d6
10
calltrace.go
10
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 ""
|
||||
}
|
||||
|
14
error.go
14
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(": ")
|
||||
|
@ -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,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user