-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
256 lines (214 loc) · 4.66 KB
/
error.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package dune
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"runtime"
"strconv"
"strings"
)
func NewCodeError(code int, msg string, args ...interface{}) *VMError {
if len(args) > 0 {
msg = fmt.Sprintf(msg, args...)
}
return &VMError{Code: code, Message: msg}
}
func Wrap(msg string, err error) error {
e, ok := err.(*VMError)
if !ok {
return fmt.Errorf("%s: %w", msg, err)
}
w := &VMError{
Message: msg,
Wrapped: e,
}
return w
}
type VMError struct {
Code int
Message string
TraceLines []TraceLine
Wrapped *VMError
IsRethrow bool
pc int
instruction *Instruction
goError error
}
func (e *VMError) Type() string {
return "Error"
}
func (e *VMError) String() string {
return e.Error()
}
func (e *VMError) ErrorMessage() string {
return e.Message
}
func (e *VMError) Error() string {
var b = &bytes.Buffer{}
b.WriteString(e.Message)
if len(e.TraceLines) > 0 {
b.WriteRune('\n')
for _, s := range e.TraceLines {
if s.Function == "" || s.Line == 0 {
continue // this is an empty position
}
fmt.Fprintf(b, " -> %s\n", s.String())
}
}
wrap := e.Wrapped
for wrap != nil {
b.WriteRune('\n')
b.WriteString(wrap.Error())
wrap = wrap.Wrapped
}
return b.String()
}
func (e *VMError) Is(msg string) bool {
if goErrorIs(e.goError, msg) {
return true
}
wrap := e.Wrapped
for wrap != nil {
if goErrorIs(wrap.goError, msg) {
return true
}
wrap = wrap.Wrapped
}
return false
}
func (e *VMError) Stack() string {
var b = &bytes.Buffer{}
for _, s := range e.TraceLines {
if s.Function == "" && s.File == "" && s.Line == 0 {
continue // this is an empty position
}
fmt.Fprintf(b, " -> %s\n", s.String())
}
return b.String()
}
// func (e *VMError) stackLines() []string {
// lines := make([]string, len(e.TraceLines))
// for _, s := range e.TraceLines {
// if s.Function == "" && s.File == "" && s.Line == 0 {
// continue // this is an empty position
// }
// lines = append(lines, s.String())
// }
// return lines
// }
func (e *VMError) GetField(name string, vm *VM) (Value, error) {
switch name {
case "code":
return NewInt(e.Code), nil
case "message":
return NewString(e.Message), nil
case "pc":
return NewInt(e.pc), nil
case "stackTrace":
return NewString(e.Stack()), nil
}
return UndefinedValue, nil
}
func (e *VMError) GetMethod(name string) NativeMethod {
switch name {
case "is":
return e.is
case "string":
return e.string
}
return nil
}
func (e *VMError) is(args []Value, vm *VM) (Value, error) {
if len(args) != 1 {
return NullValue, fmt.Errorf("expected 1 argument, got %d", len(args))
}
if args[0].Type != String {
return NullValue, fmt.Errorf("expected a string, got %s", args[0].TypeName())
}
v := e.Is(args[0].String())
return NewBool(v), nil
}
func (e *VMError) string(args []Value, vm *VM) (Value, error) {
return NewString(e.Error()), nil
}
func (e *VMError) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Code int
Message string
TraceLines []TraceLine
}{
Code: e.Code,
Message: e.Message,
TraceLines: e.TraceLines,
})
}
func goErrorIs(err error, msg string) bool {
if err == nil {
return false
}
for {
if err.Error() == msg {
return true
}
err = errors.Unwrap(err)
if err == nil {
return false
}
}
}
func Stacktrace() string {
c := callers()
return stacktrace(c)
}
func stacktrace(stack *stack) string {
var buf bytes.Buffer
for _, f := range stack.StackTrace() {
pc := f.pc()
fn := runtime.FuncForPC(pc)
if strings.HasPrefix(fn.Name(), "dune.") {
// ignore Go src
continue
}
file, _ := fn.FileLine(pc)
buf.WriteString(" -> ")
buf.WriteString(file)
buf.WriteRune(':')
buf.WriteString(strconv.Itoa(f.line()))
buf.WriteRune('\n')
}
return buf.String()
}
func callers() *stack {
const depth = 32
var pcs [depth]uintptr
n := runtime.Callers(4, pcs[:])
var st stack = pcs[0:n]
return &st
}
// Frame represents a program counter inside a stack frame.
type Frame uintptr
// pc returns the program counter for this frame;
// multiple frames may have the same PC value.
func (f Frame) pc() uintptr { return uintptr(f) - 1 }
// StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
type StackTrace []Frame
// stack represents a stack of program counters.
type stack []uintptr
func (s *stack) StackTrace() StackTrace {
f := make([]Frame, len(*s))
for i := 0; i < len(f); i++ {
f[i] = Frame((*s)[i])
}
return f
}
// line returns the line number of source code of the
// function for this Frame's pc.
func (f Frame) line() int {
fn := runtime.FuncForPC(f.pc())
if fn == nil {
return 0
}
_, line := fn.FileLine(f.pc())
return line
}