-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
52 lines (42 loc) · 1.25 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
//go:build js
package goji
import (
"reflect"
"syscall/js"
)
func init() {
Error = errorJS(js.Global().Get("Error"))
}
type errorJS js.Value
// Error is a wrapper for the Error global object.
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
var Error errorJS
// New wraps the Error constructor.
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error
func (e errorJS) New(message string) ErrorValue {
res := js.Value(e).New(message)
return ErrorValue(res)
}
var _ (error) = (ErrorValue)(js.Undefined())
// ErrorValue is an instance of
type ErrorValue js.Value
// Error wraps the Error toString prototype method.
//
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/toString
func (v ErrorValue) Error() string {
res := js.Value(v).Call("toString")
return res.String()
}
// WrapError is a helper func that returns an ErrorValue with the message
// set to the given error's `Error()` value and the name set to the
// given error's reflected type name.
func WrapError(err error) ErrorValue {
wrap := Error.New(err.Error())
name := reflect.TypeOf(err).Elem().Name()
if name != "" {
js.Value(wrap).Set("name", name)
}
return wrap
}