Something went wrong. Try again.
Error-handling utilities missing from Go's standard library.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162package inspect
import ( "runtime" "slices" "strconv" "strings" "unicode/utf8")
type stackView struct { root frameNode untraced []*stackError nextID int}
type frameNode struct { frame runtime.Frame kids []*frameNode errors []*stackError}
type stackError struct { tag string msg string causes []int id int}
func (v *stackView) add(n node, cfg config) int { v.nextID++ e := &stackError{id: v.nextID, msg: n.msg} e.tag, _ = header(n.err, cfg) if cfg.deduplicate { e.msg = ownMessage(n) } if len(n.kids) > 0 { e.causes = make([]int, 0, len(n.kids)) } stack := stackFrames(n.err) if len(stack) == 0 { v.untraced = append(v.untraced, e) } else { v.root.insert(stack, e) } for _, kid := range n.kids { e.causes = append(e.causes, v.add(kid, cfg)) } return e.id}
func (n *frameNode) insert(stack []runtime.Frame, err *stackError) { for _, frame := range slices.Backward(stack) { var next *frameNode for _, kid := range n.kids { if sameFrame(kid.frame, frame) { next = kid break } } if next == nil { next = &frameNode{frame: frame} n.kids = append(n.kids, next) } n = next } n.errors = append(n.errors, err)}
func (v *stackView) render(emit func(string) bool) bool { count := len(v.root.kids) if len(v.untraced) > 0 { count++ } for i, root := range v.root.kids { branch, guide := "", "" if count > 1 { branch, guide = connector(i == count-1) } if !renderFrameNode(root, "", branch, guide, emit) { return false } } if len(v.untraced) > 0 { branch, guide := "", "" if count > 1 { branch, guide = connector(true) } if !emit(branch + "without stack") { return false } for _, err := range v.untraced { if !renderStackError(err, guide+" ", emit) { return false } } } return true}
func renderFrameNode(n *frameNode, prefix, branch, guide string, emit func(string) bool) bool { if !emit(prefix + branch + n.frame.Function) { return false } continuation := prefix + guide if !emit(continuation + " " + n.frame.File + ":" + strconv.Itoa(n.frame.Line)) { return false } annotationPrefix := continuation + " " if len(n.kids) > 0 { annotationPrefix = continuation + "│ " } for _, err := range n.errors { if !renderStackError(err, annotationPrefix, emit) { return false } } for i, kid := range n.kids { childBranch, childGuide := connector(i == len(n.kids)-1) if !renderFrameNode(kid, continuation, childBranch, childGuide, emit) { return false } } return true}
func renderStackError(err *stackError, prefix string, emit func(string) bool) bool { label := "#" + strconv.Itoa(err.id) if err.tag != "" { label += " " + err.tag } var causes strings.Builder if len(err.causes) > 0 { causes.WriteString(" → causes ") for i, id := range err.causes { if i > 0 { causes.WriteString(", ") } causes.WriteByte('#') causes.WriteString(strconv.Itoa(id)) } } i := 0 for text := range strings.SplitSeq(err.msg, "\n") { line := prefix if i == 0 { line += label if text != "" { line += " " + text } line += causes.String() } else { line += strings.Repeat(" ", utf8.RuneCountInString(label)+1) + text } if !emit(line) { return false } i++ } return true}