-
Notifications
You must be signed in to change notification settings - Fork 19
/
errors.go
438 lines (410 loc) · 11.8 KB
/
errors.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Copyright (c) 2019 Open2b Software Snc. All rights reserved.
// https://www.open2b.com
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package runtime
import (
"reflect"
"runtime"
"strconv"
"strings"
)
var errNilPointer = runtimeError("runtime error: invalid memory address or nil pointer dereference")
// fatalError represents a fatal error. A fatal error cannot be recovered by
// the running program.
type fatalError struct {
env *env
msg interface{}
pos Position
path string
}
func (err *fatalError) Error() string {
return "fatal error: " + panicToString(err.msg)
}
// runtimeError represents a runtime error.
type runtimeError string
func (err runtimeError) Error() string { return string(err) }
func (err runtimeError) RuntimeError() {}
// outError represents an error occurred calling the Write method of a
// template output.
type outError struct {
err error
}
func (err outError) Error() string {
return "out error: " + err.err.Error()
}
// errTypeAssertion returns a runtime error for a failed type assertion x.(T).
// interfaceType is the type of x, dynamicType is dynamic type of x or nil if
// x is nil and assertedType is the type T. If T is an interface type,
// missingMethod is the missing method name.
//
// The Go runtime returns a runtime.TypeAssertionError, Scriggo cannot return
// an error with this type because it has unexported fields. See also:
// https://github.com/golang/go/issues/14443
func errTypeAssertion(interfaceType, dynamicType, assertedType reflect.Type, missingMethod string) runtimeError {
s := "interface conversion: "
if dynamicType == nil {
if assertedType.Kind() == reflect.Interface {
return runtimeError(s + "interface is nil, not " + assertedType.String())
}
return runtimeError(s + interfaceType.String() + " is nil, not " + assertedType.String())
}
if missingMethod != "" {
return runtimeError(s + dynamicType.String() + " is not " + assertedType.String() +
": missing method " + missingMethod)
}
s += interfaceType.String() + " is " + dynamicType.String() + ", not " + assertedType.String()
if dynamicType.String() != assertedType.String() {
return runtimeError(s)
}
s += " (types from different "
if dynamicType.PkgPath() == interfaceType.PkgPath() {
return runtimeError(s + "scopes)")
}
return runtimeError(s + "packages)")
}
// exitError represents an exit error.
type exitError int
func (err exitError) Error() string {
return "exit code " + strconv.Itoa(int(err))
}
// errIndexOutOfRange returns an index of range runtime error for the
// currently running virtual machine instruction.
func (vm *VM) errIndexOutOfRange() runtimeError {
in := vm.fn.Body[vm.pc-1]
var index, length int
switch in.Op {
case OpAddr, OpIndex, -OpIndex, OpIndexRef, -OpIndexRef:
index = int(vm.intk(in.B, in.Op < 0))
length = vm.general(in.A).Len()
case OpIndexString, -OpIndexString:
index = int(vm.intk(in.B, in.Op < 0))
length = len(vm.string(in.A))
case OpSetSlice, -OpSetSlice:
index = int(vm.int(in.C))
length = vm.general(in.B).Len()
default:
panic("unexpected operation")
}
s := "runtime error: index out of range [" + strconv.Itoa(index) + "] with length " + strconv.Itoa(length)
return runtimeError(s)
}
// newPanic returns a new *PanicError with the given error message.
func (vm *VM) newPanic(msg interface{}) *PanicError {
return &PanicError{
message: msg,
path: vm.fn.DebugInfo[vm.pc].Path,
position: vm.fn.DebugInfo[vm.pc].Position,
}
}
// convertPanic converts a panic to an error.
func (vm *VM) convertPanic(msg interface{}) error {
switch err := msg.(type) {
case exitError:
return err
case outError:
return vm.newPanic(err)
}
switch op := vm.fn.Body[vm.pc-1].Op; op {
case OpAddr, OpIndex, -OpIndex, OpIndexRef, -OpIndexRef, OpSetSlice, -OpSetSlice:
switch err := msg.(type) {
case runtime.Error:
if s := err.Error(); strings.HasPrefix(s, "runtime error: index out of range") {
return vm.newPanic(runtimeError(s))
}
case string:
if err == "reflect: slice index out of range" || err == "reflect: array index out of range" {
return vm.newPanic(vm.errIndexOutOfRange())
}
}
case OpAppendSlice:
if err, ok := msg.(string); ok && err == "reflect.Append: slice overflow" {
return vm.newPanic(runtimeError("append: out of memory"))
}
case OpCallIndirect:
in := vm.fn.Body[vm.pc-1]
v := vm.general(in.A)
if !v.IsValid() || !v.CanInterface() {
break
}
if f, ok := v.Interface().(*callable); !ok || f.fn != nil {
break
}
fallthrough
case OpCallNative:
switch msg := msg.(type) {
case runtimeError:
break
case *fatalError:
// TODO: check env.
return msg
case runtime.Error:
// TODO: check env.
break
default:
return vm.newPanic(msg)
}
case OpClose:
if err, ok := msg.(runtime.Error); ok {
switch s := err.Error(); s {
case "close of closed channel", "close of nil channel":
return vm.newPanic(runtimeError(s))
}
}
case OpConvert:
if err, ok := msg.(string); ok && strings.HasPrefix(err, "reflect: cannot convert slice with length") {
return vm.newPanic(runtimeError("runtime error:" + err[len("reflect:"):]))
}
case OpDelete:
if err, ok := msg.(runtime.Error); ok {
if s := err.Error(); strings.HasPrefix(s, "runtime error: hash of unhashable type ") {
return vm.newPanic(runtimeError(s))
}
}
case OpDivInt, OpDiv, OpRemInt, OpRem:
if err, ok := msg.(runtime.Error); ok {
if s := err.Error(); s == "runtime error: integer divide by zero" {
return vm.newPanic(runtimeError(s))
}
}
case OpIf, -OpIf:
if err, ok := msg.(runtime.Error); ok {
if s := err.Error(); strings.HasPrefix(s, "runtime error: comparing uncomparable type ") {
return vm.newPanic(runtimeError(s))
}
}
case OpIndexString, -OpIndexString:
if err, ok := msg.(runtime.Error); ok {
if s := err.Error(); strings.HasPrefix(s, "runtime error: index out of range") {
return vm.newPanic(runtimeError(s))
}
}
case OpMakeChan, -OpMakeChan:
if err, ok := msg.(string); ok && err == "reflect.MakeChan: negative buffer size" {
return vm.newPanic(runtimeError("makechan: size out of range"))
}
case OpMakeSlice:
if err, ok := msg.(string); ok {
switch err {
case "reflect.MakeSlice: negative len":
return vm.newPanic(runtimeError("runtime error: makeslice: len out of range"))
case "reflect.MakeSlice: negative cap", "reflect.MakeSlice: len > cap":
return vm.newPanic(runtimeError("runtime error: makeslice: cap out of range"))
}
}
case OpPanic:
return vm.newPanic(msg)
case OpSend, -OpSend:
switch err := msg.(type) {
case runtime.Error:
if s := err.Error(); s == "send on closed channel" {
return vm.newPanic(runtimeError(s))
}
case string:
if err == "close of nil channel" {
return vm.newPanic(runtimeError(err))
}
}
case OpSetMap, -OpSetMap:
if err, ok := msg.(runtime.Error); ok {
s := err.Error()
if s == "assignment to entry in nil map" ||
strings.HasPrefix(s, "runtime error: hash of unhashable type ") {
return vm.newPanic(runtimeError(s))
}
}
case OpSlice, OpStringSlice:
// https://github.com/open2b/scriggo/issues/321
switch err := msg.(type) {
case runtime.Error:
if s := err.Error(); strings.HasPrefix(s, "runtime error: slice bounds out of range") {
return vm.newPanic(runtimeError("runtime error: slice bounds out of range"))
}
case string:
if err == "reflect.Value.Slice3: slice index out of bounds" {
return vm.newPanic(runtimeError("runtime error: slice bounds out of range"))
}
}
}
if _, ok := msg.(runtimeError); ok {
return vm.newPanic(msg)
}
return &fatalError{msg: msg}
}
type PanicError struct {
message interface{}
recovered bool
stackTrace []byte
next *PanicError
path string
position Position
}
// Error returns all currently active panics as a string.
//
// To print only the message, use the String method instead.
func (p *PanicError) Error() string {
var s string
for p != nil {
s = "\n" + s
if p.Recovered() {
s = " [recovered]" + s
}
s = p.String() + s
if p.Next() != nil {
s = "\tpanic: " + s
}
p = p.Next()
}
return s
}
// Message returns the message.
func (p *PanicError) Message() interface{} {
return p.message
}
// Next returns the next panic in the chain.
func (p *PanicError) Next() *PanicError {
return p.next
}
// Recovered reports whether it has been recovered.
func (p *PanicError) Recovered() bool {
return p.recovered
}
// String returns the message as a string.
func (p *PanicError) String() string {
return panicToString(p.message)
}
// Path returns the path of the file that panicked.
func (p *PanicError) Path() string {
return p.path
}
// Position returns the position.
func (p *PanicError) Position() Position {
return p.position
}
func panicToString(msg interface{}) string {
switch v := msg.(type) {
case nil:
return "nil"
case bool:
if v {
return "true"
}
return "false"
case int:
return strconv.Itoa(v)
case int8:
return strconv.Itoa(int(v))
case int16:
return strconv.Itoa(int(v))
case int32:
return strconv.Itoa(int(v))
case int64:
return strconv.FormatInt(v, 10)
case uint:
return strconv.FormatUint(uint64(v), 10)
case uint8:
return strconv.FormatUint(uint64(v), 10)
case uint16:
return strconv.FormatUint(uint64(v), 10)
case uint32:
return strconv.FormatUint(uint64(v), 10)
case uint64:
return strconv.FormatUint(v, 10)
case uintptr:
return strconv.FormatUint(uint64(v), 10)
case float32:
return strconv.FormatFloat(float64(v), 'e', -1, 32)
case float64:
return strconv.FormatFloat(v, 'e', -1, 64)
case complex64:
return "(" + strconv.FormatFloat(float64(real(v)), 'e', -1, 32) +
strconv.FormatFloat(float64(imag(v)), 'e', -1, 32) + ")"
case complex128:
return "(" + strconv.FormatFloat(real(v), 'e', 3, 64) +
strconv.FormatFloat(imag(v), 'e', 3, 64) + ")"
case string:
return v
case error:
return v.Error()
case stringer:
return v.String()
default:
rv := reflect.ValueOf(v)
rt := rv.Type().String()
var s string
switch rv.Kind() {
case reflect.Bool:
s = "false"
if rv.Bool() {
s = "true"
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
s = strconv.FormatInt(rv.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
s = strconv.FormatUint(rv.Uint(), 10)
case reflect.Float32:
s = strconv.FormatFloat(rv.Float(), 'e', -1, 32)
case reflect.Float64:
s = strconv.FormatFloat(rv.Float(), 'e', -1, 64)
case reflect.Complex64:
c := rv.Complex()
s = strconv.FormatFloat(real(c), 'e', -1, 32) +
strconv.FormatFloat(imag(c), 'e', -1, 32)
case reflect.Complex128:
c := rv.Complex()
s = strconv.FormatFloat(real(c), 'e', 3, 64) +
strconv.FormatFloat(imag(c), 'e', 3, 64)
case reflect.String:
s = `"` + rv.String() + `"`
default:
iData := reflect.ValueOf(&v).Elem().InterfaceData()
return "(" + rt + ") (" + hex(iData[0]) + "," + hex(iData[1]) + ")"
}
return rt + "(" + s + ")"
}
}
// missingMethod returns a method in iface and not in typ.
func missingMethod(typ reflect.Type, iface reflect.Type) string {
num := iface.NumMethod()
for i := 0; i < num; i++ {
mi := iface.Method(i)
mt, ok := typ.MethodByName(mi.Name)
if !ok {
return mi.Name
}
numIn := mi.Type.NumIn()
numOut := mi.Type.NumOut()
if mt.Type.NumIn()-1 != numIn || mt.Type.NumOut() != numOut {
return mi.Name
}
for j := 0; j < numIn; j++ {
if mt.Type.In(j+1) != mi.Type.In(j) {
return mi.Name
}
}
for j := 0; j < numOut; j++ {
if mt.Type.Out(j) != mi.Type.Out(j) {
return mi.Name
}
}
}
return ""
}
type stringer interface {
String() string
}
func hex(p uintptr) string {
i := 20
h := [20]byte{}
for {
i--
h[i] = "0123456789abcdef"[p%16]
p = p / 16
if p == 0 {
break
}
}
h[i-1] = 'x'
h[i-2] = '0'
return string(h[i-2:])
}