-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
65 lines (56 loc) · 1.17 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
package runtime
import (
"fmt"
"strings"
)
type Stack struct {
FileName string
Line int
Function string
}
type Exception interface {
GetStack() []Stack
GetMessage() string
MessageWithStack() string
Error() string
}
type RuntimeError struct {
Stack []Stack
Message string
}
func (e *RuntimeError) Error() string {
return e.GetMessage()
}
func (e *RuntimeError) GetStack() []Stack {
return e.Stack
}
func (e *RuntimeError) GetMessage() string {
return e.Message
}
func (e *RuntimeError) MessageWithStack() string {
var builder strings.Builder
builder.WriteString("Exception! " + e.Message + "\n")
for _, s := range e.Stack {
builder.WriteString(fmt.Sprintf("%s:%d (%s)\n", s.FileName, s.Line, s.Function))
}
return builder.String()
}
func ExceptionToValue(e Exception, c *Context) Value {
if e == nil {
return constNil
}
v := NewObject()
v.SetMember("message", NewStr(e.GetMessage()), c)
if re, ok := e.(*RuntimeError); ok {
stack := NewArray(len(re.Stack))
for _, s := range re.Stack {
stack.PushBack(NewArrayByValues(
NewStr(s.FileName),
NewInt(int64(s.Line)),
NewStr(s.Function),
))
}
v.SetMember("stack", stack, c)
}
return v
}