forked from ethereum/go-ethereum
/
error.go
152 lines (133 loc) · 3.36 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
package otto
import (
"errors"
"fmt"
"github.com/robertkrimen/otto/ast"
)
type _exception struct {
value interface{}
}
func newException(value interface{}) *_exception {
return &_exception{
value: value,
}
}
func (self *_exception) eject() interface{} {
value := self.value
self.value = nil // Prevent Go from holding on to the value, whatever it is
return value
}
type _error struct {
Name string
Message string
Line int // Hackish -- line where the error/exception occurred
}
var messageDetail map[string]string = map[string]string{
"notDefined": "%v is not defined",
}
func messageFromDescription(description string, argumentList ...interface{}) string {
message := messageDetail[description]
if message == "" {
message = description
}
message = fmt.Sprintf(message, argumentList...)
return message
}
func (self _error) MessageValue() Value {
if self.Message == "" {
return UndefinedValue()
}
return toValue_string(self.Message)
}
func (self _error) String() string {
if len(self.Name) == 0 {
return self.Message
}
if len(self.Message) == 0 {
return self.Name
}
return fmt.Sprintf("%s: %s", self.Name, self.Message)
}
func newError(name string, argumentList ...interface{}) _error {
description := ""
var node ast.Node = nil
length := len(argumentList)
if length > 0 {
if node, _ = argumentList[length-1].(ast.Node); node != nil || argumentList[length-1] == nil {
argumentList = argumentList[0 : length-1]
length -= 1
}
if length > 0 {
description, argumentList = argumentList[0].(string), argumentList[1:]
}
}
return _error{
Name: name,
Message: messageFromDescription(description, argumentList...),
Line: -1,
}
//error := _error{
// Name: name,
// Message: messageFromDescription(description, argumentList...),
// Line: -1,
//}
//if node != nil {
// error.Line = ast.position()
//}
//return error
}
func newReferenceError(argumentList ...interface{}) _error {
return newError("ReferenceError", argumentList...)
}
func newTypeError(argumentList ...interface{}) _error {
return newError("TypeError", argumentList...)
}
func newRangeError(argumentList ...interface{}) _error {
return newError("RangeError", argumentList...)
}
func newSyntaxError(argumentList ...interface{}) _error {
return newError("SyntaxError", argumentList...)
}
func newURIError(argumentList ...interface{}) _error {
return newError("URIError", argumentList...)
}
func typeErrorResult(throw bool) bool {
if throw {
panic(newTypeError())
}
return false
}
func catchPanic(function func()) (err error) {
// FIXME
defer func() {
if caught := recover(); caught != nil {
if exception, ok := caught.(*_exception); ok {
caught = exception.eject()
}
switch caught := caught.(type) {
//case *_syntaxError:
// err = errors.New(fmt.Sprintf("%s (line %d)", caught.String(), caught.Line+0))
// return
case _error:
if caught.Line == -1 {
err = errors.New(caught.String())
} else {
// We're 0-based (for now), hence the + 1
err = errors.New(fmt.Sprintf("%s (line %d)", caught.String(), caught.Line+1))
}
return
case Value:
err = errors.New(toString(caught))
return
//case string:
// if strings.HasPrefix(caught, "SyntaxError:") {
// err = errors.New(caught)
// return
// }
}
panic(caught)
}
}()
function()
return nil
}