Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set get frames public #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 {
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