-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
112 lines (95 loc) · 2.68 KB
/
util.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
// Package common contains helpers for interacting with the JavaScript runtime.
package common
import (
"bytes"
"fmt"
"io"
"strings"
"github.com/dop251/goja"
"github.com/sirupsen/logrus"
)
func findLocationError(errorString string) string {
// Find " at " in the error string
if !strings.Contains(errorString, " at ") {
return ""
}
// Split on the first occurrence of " at "
splitString := strings.SplitN(errorString, " at ", 2)
// If we have two elements, the second is the location
if len(splitString) != 2 {
return ""
}
// Get second element
location := splitString[1]
// Look for at *.js:*
if !strings.Contains(location, ".js:") {
return ""
}
// Split on the first occurrence of ".js:"
splitString = strings.SplitN(location, ".js:", 2)
if len(splitString) != 2 {
return ""
}
// Get first element
filename := fmt.Sprintf("%s.js", splitString[0])
// Get second element
lineNumber := splitString[1]
return fmt.Sprintf("%s:%s", filename, lineNumber)
}
// Throw a JS error; avoids re-wrapping GoErrors.
func Throw(rt *goja.Runtime, err error) {
if e, ok := err.(*goja.Exception); ok { //nolint:errorlint // we don't really want to unwrap here
panic(e)
}
panic(rt.ToValue(err))
}
// GetReader tries to return an io.Reader value from an exported goja value.
func GetReader(data interface{}) (io.Reader, error) {
switch r := data.(type) {
case string:
return bytes.NewBufferString(r), nil
case []byte:
return bytes.NewBuffer(r), nil
case io.Reader:
return r, nil
case goja.ArrayBuffer:
return bytes.NewBuffer(r.Bytes()), nil
default:
return nil, fmt.Errorf("invalid type %T, it needs to be a string, byte array or an ArrayBuffer", data)
}
}
// ToBytes tries to return a byte slice from compatible types.
func ToBytes(data interface{}) ([]byte, error) {
switch dt := data.(type) {
case []byte:
return dt, nil
case string:
return []byte(dt), nil
case goja.ArrayBuffer:
return dt.Bytes(), nil
default:
return nil, fmt.Errorf("invalid type %T, expected string, []byte or ArrayBuffer", data)
}
}
// ToString tries to return a string from compatible types.
func ToString(data interface{}) (string, error) {
switch dt := data.(type) {
case []byte:
return string(dt), nil
case string:
return dt, nil
case goja.ArrayBuffer:
return string(dt.Bytes()), nil
default:
return "", fmt.Errorf("invalid type %T, expected string, []byte or ArrayBuffer", data)
}
}
// RunWithPanicCatching catches panic and converts into an InterruptError error that should abort a script
func RunWithPanicCatching(logger logrus.FieldLogger, rt *goja.Runtime, fn func() error) (err error) {
defer func() {
if r := recover(); r != nil {
logger.Error(r)
}
}()
return fn()
}