Skip to content

Commit

Permalink
Set get frames public
Browse files Browse the repository at this point in the history
  • Loading branch information
ldreux committed Jan 21, 2023
1 parent 9f7c73f commit 540884d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
20 changes: 17 additions & 3 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions stackframe.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"runtime"
)

type frame interface {
type Frame interface {
Function() string
File() string
Line() int
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions stacktrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down

0 comments on commit 540884d

Please sign in to comment.