-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.go
57 lines (47 loc) · 1.13 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
package jopher
import (
"errors"
"github.com/gopherjs/gopherjs/js"
)
// CatchPanic catches a panic and puts the error into the supplied error. This should be called in a
// defer.
func CatchPanic(returnedError *error, failureMessage string) {
// Catch the panic
err := recover()
if err == nil {
return
}
// Try to make it a JS error
jsErr, ok := err.(*js.Error)
if ok {
*returnedError = jsErr
}
// Try to make it a go error
goErr, ok := err.(error)
if ok {
*returnedError = goErr
}
// Not a go or JS error
*returnedError = errors.New(failureMessage)
}
// CallOnPanic calls the supplied function when a panic is recovered. This should be called in a defer.
func CallOnPanic(reject func(interface{})) {
err := recover()
if err != nil {
reject(err)
}
}
// Throw throws the supplied javascript object.
func Throw(object *js.Object) {
panic(object)
}
// ThrowOnError throws when the supplied error is not nil.
func ThrowOnError(err error) {
if err != nil {
panic(NewError(err.Error()))
}
}
// NewError creates a new JS error.
func NewError(message string) *js.Object {
return js.Global.Get("Error").New(message)
}