cosmos-sdk/errors/stacktrace.go

122 lines
2.9 KiB
Go
Raw Normal View History

package errors
import (
"fmt"
"io"
"runtime"
"strings"
"github.com/pkg/errors"
)
func matchesFunc(f errors.Frame, prefixes ...string) bool {
fn := funcName(f)
for _, prefix := range prefixes {
if strings.HasPrefix(fn, prefix) {
return true
}
}
return false
}
// funcName returns the name of this function, if known.
func funcName(f errors.Frame) string {
// this looks a bit like magic, but follows example here:
// https://github.com/pkg/errors/blob/v0.8.1/stack.go#L43-L50
pc := uintptr(f) - 1
fn := runtime.FuncForPC(pc)
if fn == nil {
return "unknown"
}
return fn.Name()
}
func fileLine(f errors.Frame) (string, int) {
// this looks a bit like magic, but follows example here:
// https://github.com/pkg/errors/blob/v0.8.1/stack.go#L14-L27
// as this is where we get the Frames
pc := uintptr(f) - 1
fn := runtime.FuncForPC(pc)
if fn == nil {
return "unknown", 0
}
return fn.FileLine(pc)
}
func trimInternal(st errors.StackTrace) errors.StackTrace {
// trim our internal parts here
// manual error creation, or runtime for caught panics
for matchesFunc(st[0],
// where we create errors
"github.com/cosmos/cosmos-sdk/errors.Wrap",
"github.com/cosmos/cosmos-sdk/errors.Wrapf",
"github.com/cosmos/cosmos-sdk/errors.WithType",
// runtime are added on panics
"runtime.",
// _test is defined in coverage tests, causing failure
// "/_test/"
) {
st = st[1:]
}
// trim out outer wrappers (runtime.goexit and test library if present)
for l := len(st) - 1; l > 0 && matchesFunc(st[l], "runtime.", "testing."); l-- {
st = st[:l]
}
return st
}
func writeSimpleFrame(s io.Writer, f errors.Frame) {
file, line := fileLine(f)
// cut file at "github.com/"
// TODO: generalize better for other hosts?
chunks := strings.SplitN(file, "github.com/", 2)
if len(chunks) == 2 {
file = chunks[1]
}
feat: standalone errors go.mod (#10779) ## Description This PR: * moves all of the `types/errors` code to a new `errors` go module, except: * the `RootCodespace` errors in `types/errors` stay there * ABCI stuff that depends on tendermint stays in `types/errors * adds aliases to everything in `types/errors` referencing `errors` so **this is not a breaking change** This will allow standalone go modules to use the same error types as the SDK. In particular, I want the `orm` to reference `errors` and then the SDK will be able to import `orm` and it can stay standalone. The same could apply to the `db` module. After this PR the plan is to: * tag `github.com/cosmos/cosmos-sdk/errors` as `v1.0` 🎉 * remove the `replace` directive for `errors` in the main SDK `go.mod` --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-12-17 13:53:08 -08:00
_, _ = fmt.Fprintf(s, " [%s:%d]", file, line)
}
// Format works like pkg/errors, with additions.
// %s is just the error message
// %+v is the full stack trace
// %v appends a compressed [filename:line] where the error
// was created
//
// Inspired by https://github.com/pkg/errors/blob/v0.8.1/errors.go#L162-L176
func (e *wrappedError) Format(s fmt.State, verb rune) {
// normal output here....
if verb != 'v' {
feat: standalone errors go.mod (#10779) ## Description This PR: * moves all of the `types/errors` code to a new `errors` go module, except: * the `RootCodespace` errors in `types/errors` stay there * ABCI stuff that depends on tendermint stays in `types/errors * adds aliases to everything in `types/errors` referencing `errors` so **this is not a breaking change** This will allow standalone go modules to use the same error types as the SDK. In particular, I want the `orm` to reference `errors` and then the SDK will be able to import `orm` and it can stay standalone. The same could apply to the `db` module. After this PR the plan is to: * tag `github.com/cosmos/cosmos-sdk/errors` as `v1.0` 🎉 * remove the `replace` directive for `errors` in the main SDK `go.mod` --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-12-17 13:53:08 -08:00
_, _ = fmt.Fprint(s, e.Error())
return
}
// work with the stack trace... whole or part
stack := trimInternal(stackTrace(e))
if s.Flag('+') {
feat: standalone errors go.mod (#10779) ## Description This PR: * moves all of the `types/errors` code to a new `errors` go module, except: * the `RootCodespace` errors in `types/errors` stay there * ABCI stuff that depends on tendermint stays in `types/errors * adds aliases to everything in `types/errors` referencing `errors` so **this is not a breaking change** This will allow standalone go modules to use the same error types as the SDK. In particular, I want the `orm` to reference `errors` and then the SDK will be able to import `orm` and it can stay standalone. The same could apply to the `db` module. After this PR the plan is to: * tag `github.com/cosmos/cosmos-sdk/errors` as `v1.0` 🎉 * remove the `replace` directive for `errors` in the main SDK `go.mod` --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-12-17 13:53:08 -08:00
_, _ = fmt.Fprintf(s, "%+v\n", stack)
_, _ = fmt.Fprint(s, e.Error())
} else {
feat: standalone errors go.mod (#10779) ## Description This PR: * moves all of the `types/errors` code to a new `errors` go module, except: * the `RootCodespace` errors in `types/errors` stay there * ABCI stuff that depends on tendermint stays in `types/errors * adds aliases to everything in `types/errors` referencing `errors` so **this is not a breaking change** This will allow standalone go modules to use the same error types as the SDK. In particular, I want the `orm` to reference `errors` and then the SDK will be able to import `orm` and it can stay standalone. The same could apply to the `db` module. After this PR the plan is to: * tag `github.com/cosmos/cosmos-sdk/errors` as `v1.0` 🎉 * remove the `replace` directive for `errors` in the main SDK `go.mod` --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-12-17 13:53:08 -08:00
_, _ = fmt.Fprint(s, e.Error())
writeSimpleFrame(s, stack[0])
}
}
// stackTrace returns the first found stack trace frame carried by given error
// or any wrapped error. It returns nil if no stack trace is found.
func stackTrace(err error) errors.StackTrace {
type stackTracer interface {
StackTrace() errors.StackTrace
}
for {
if st, ok := err.(stackTracer); ok {
return st.StackTrace()
}
if c, ok := err.(causer); ok {
err = c.Cause()
} else {
return nil
}
}
}