From 540884d8476dc1046b449ee68b87bb0a472c7b48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Dreux?= Date: Sat, 21 Jan 2023 09:36:36 +0100 Subject: [PATCH] Set get frames public --- error.go | 20 +++++++++++++++++--- stackframe.go | 6 +++--- stacktrace.go | 4 ++-- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/error.go b/error.go index 7cca0b8..7e5cf6c 100644 --- a/error.go +++ b/error.go @@ -170,9 +170,9 @@ func (e *Error) Unwrap() error { // Format implements the Formatter interface. // Supported verbs: // -// %s simple message output -// %v same as %s -// %+v full output complete with a stack trace +// %s simple message output +// %v same as %s +// %+v full output complete with a stack trace // // In is nearly always preferable to use %+v format. // If a stack trace is not required, it should be omitted at the moment of creation rather in formatting. @@ -189,6 +189,20 @@ func (e *Error) Format(s fmt.State, verb rune) { } } +// GetFrames export stacktrace as frame slice. +func (e *Error) GetFrames() []Frame { + if e.stackTrace == nil { + return nil + } + + pc, _ := e.stackTrace.deduplicateFramesWithCause() + if len(pc) == 0 { + return nil + } + + return frameHelperSingleton.GetFrames(pc) +} + // Error implements the error interface. // A result is the same as with %s formatter and does not contain a stack trace. func (e *Error) Error() string { diff --git a/stackframe.go b/stackframe.go index db95f47..6053856 100644 --- a/stackframe.go +++ b/stackframe.go @@ -4,7 +4,7 @@ import ( "runtime" ) -type frame interface { +type Frame interface { Function() string File() string Line() int @@ -31,9 +31,9 @@ func (f *defaultFrame) Line() int { return f.frame.Line } -func (c *frameHelper) GetFrames(pcs []uintptr) []frame { +func (c *frameHelper) GetFrames(pcs []uintptr) []Frame { frames := runtime.CallersFrames(pcs[:]) - result := make([]frame, 0, len(pcs)) + result := make([]Frame, 0, len(pcs)) var rawFrame runtime.Frame next := true diff --git a/stacktrace.go b/stacktrace.go index 4e8a1d2..5c1d1c3 100644 --- a/stacktrace.go +++ b/stacktrace.go @@ -55,8 +55,8 @@ var transformStackTraceLineNoop StackTraceFilePathTransformer = func(line string const ( stackTraceDepth = 128 - // tuned so that in all control paths of error creation the first frame is useful - // that is, the frame where New/Wrap/Decorate etc. are called; see TestStackTraceStart + // tuned so that in all control paths of error creation the first Frame is useful + // that is, the Frame where New/Wrap/Decorate etc. are called; see TestStackTraceStart skippedFrames = 6 )