From 23be470d9a6b901f307138638b7d178f6e713fb1 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 +++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/error.go b/error.go index 7cca0b8..698aba0 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 exports 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