Skip to content

Instantly share code, notes, and snippets.

@kamilogorek
Last active June 4, 2019 11:34
Show Gist options
  • Save kamilogorek/4b16496f82920cd61826b14af3101034 to your computer and use it in GitHub Desktop.
Save kamilogorek/4b16496f82920cd61826b14af3101034 to your computer and use it in GitHub Desktop.
func ExtractStacktrace(err error) *Stacktrace {
// https://github.com/pkg/errors
// Packages definitions:
// type Frame []uintptr
// type StackTrace []Frame
type stackTracer interface {
StackTrace() errors.StackTrace
}
var pcs []uintptr
if stacktrace, ok := err.(stackTracer); ok {
// won't compile, as types differ - hard dependency
pcs = stacktrace.StackTrace()
}
stacktrace := Stacktrace{
Frames: extractFrames(pcs),
}
return &stacktrace
}
func ExtractStacktrace(err error) *Stacktrace {
// https://github.com/pkg/errors
// Packages definitions:
// type Frame []uintptr
// type StackTrace []Frame
type stackTracer interface {
StackTrace() errors.StackTrace
}
var pcs []uintptr
if stacktrace, ok := err.(stackTracer); ok {
// will compile, but requires type coercion - hard dependency
st := stacktrace.StackTrace()
for _, val := range st {
pcs = append(pcs, uintptr(val))
}
}
stacktrace := Stacktrace{
Frames: extractFrames(pcs),
}
return &stacktrace
}
func ExtractStacktrace(err error) *Stacktrace {
// https://github.com/pkg/errors
// Packages definitions:
// type Frame []uintptr
// type StackTrace []Frame
type stackTracer interface {
StackTrace() []uintptr
}
var pcs []uintptr
if stacktrace, ok := err.(stackTracer); ok {
// won't read pcs, as interface is not satisfied - no dependency
pcs = stacktrace.StackTrace()
}
stacktrace := Stacktrace{
Frames: extractFrames(pcs),
}
return &stacktrace
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment