Skip to content

Support for obtaining a stack trace without requiring this package #9

@mitar

Description

@mitar

I made a spiritual successor of github.com/pkg/errors some time ago and in it I want to know if an error already contains a stack trace. The goal is that if a large codebase uses different packages for errors, they should work together and not hide existing recorded information, like a stack trace.

From what I am seeing in this package is that it has the same issue as github.com/pkg/errors: the return value of StackTrace() is a custom type defined in this package. So it is not possible to detect the error having this method without importing this package.

I would suggest that instead, the following interface is implemented:

type stackTracer interface {
        StackTrace() []uintptr
}

Then, it is easy to obtain a stack trace, use it, format it, and one does not have to depend on a library which created the error.

To prevent a breaking change, I have seen also the following interface being used in this package:

type goErrorsStackTracer interface {
	Callers() []uintptr
}

BTW, you are also using runtime.FuncForPC which is deprecated. And calling it runtime.FuncForPC at the time of making a stack trace is quite a performance hit. It is much better to just record a stack trace (into []uintptr) when making an error and then resolve addresses with runtime.FuncForPC when the stack is formatted (if ever).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions