/
model.go
123 lines (106 loc) · 2.94 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package errdisplay
import (
"fmt"
"strings"
"github.com/DomBlack/bubble-shell/internal/config"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/cockroachdb/errors"
)
type Model struct {
cfg *config.Config
err error
frames []Frame
longestFunc int
}
// New creates a new error display model
//
// If cfg is nil then the default config will be used
func New(cfg *config.Config, err error) tea.Model {
if cfg == nil {
cfg = config.Default()
}
if err == nil {
err = errors.New("nil error passed to errdisplay.New")
}
originalFrames := DeepestStack(err)
frames := FilterCommonFrames(cfg, originalFrames)
thisModule, _ := modulePackageAndFilePath()
// Find the longest function name that we'll render
// so we can pad the others to match
longestFunc := 0
for i, frame := range frames {
// If the frame package is in this binary, then trim it just to show
// the user the package path within their project
if strings.HasPrefix(frame.Package, thisModule+"/") {
frame.Package = strings.TrimPrefix(frame.Package, thisModule+"/")
frames[i] = frame
}
if frame.Package == "main" {
// On the main package we don't need to show the package name
if len(frame.Function) > longestFunc {
longestFunc = len(frame.Function)
}
} else {
fName := fmt.Sprintf("%s.%s", frame.Package, frame.Function)
if len(fName) > longestFunc {
longestFunc = len(fName)
}
}
}
return Model{
cfg: cfg,
err: err,
frames: frames,
longestFunc: longestFunc,
}
}
func (m Model) Init() tea.Cmd {
return nil
}
func (m Model) Update(_ tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}
func (m Model) View() string {
lines := []string{
m.cfg.Styles.ErrorTitle.Render("Error:") + " " + m.cfg.Styles.ErrorMessage.Render(m.err.Error()),
}
for _, detail := range errors.GetAllDetails(m.err) {
lines = append(lines, m.cfg.Styles.ErrorDetails.Render(detail))
}
// Print the stack trace
if len(m.frames) > 0 {
lines = append(lines, " ", "Stack trace:")
count := 0
for _, frame := range m.frames {
if frame.Package == "main" {
lines = append(lines,
fmt.Sprintf(
" at %s%s %s:%d",
m.cfg.Styles.StackFrameFunction.Render(frame.Function),
strings.Repeat(" ", m.longestFunc-len(frame.Function)),
FilePathFromFrame(frame),
frame.Line,
),
)
} else {
lines = append(lines,
fmt.Sprintf(
" at %s.%s%s %s:%d",
m.cfg.Styles.StackFramePackage.Render(frame.Package),
m.cfg.Styles.StackFrameFunction.Render(frame.Function),
strings.Repeat(" ", m.longestFunc-(len(frame.Function)+len(frame.Package)+1)),
FilePathFromFrame(frame),
frame.Line,
),
)
}
count++
if count >= m.cfg.MaxStackFrames {
lines = append(lines, fmt.Sprintf("... remaining %d frames omitted ...", len(m.frames)-count))
break
}
}
}
return lipgloss.JoinVertical(lipgloss.Top, lines...)
}