-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy patherrors.go
More file actions
57 lines (50 loc) · 1.82 KB
/
errors.go
File metadata and controls
57 lines (50 loc) · 1.82 KB
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 gojs
// #include <stdlib.h>
// #include <JavaScriptCore/JSStringRef.h>
// #include <JavaScriptCore/JSObjectRef.h>
// #include <JavaScriptCore/JSValueRef.h>
// #include "callback.h"
import "C"
// NewError constructs a new JavaScript Error object with message.
func (ctx *Context) NewError(message string) (*Object, error) {
errVal := ctx.newErrorValue()
msg := ctx.NewStringValue(message)
ret := C.JSObjectMakeError(ctx.ref, C.size_t(1), &msg.ref, &errVal.ref)
if errVal.ref != nil {
return nil, errVal
}
return ctx.newObject(ret), nil
}
func (ctx *Context) newErrorOrPanic(message string) C.JSValueRef {
obj, err := ctx.NewError(message)
if err != nil {
panic("newErrorOrPanic: " + err.Error())
}
// Any JSObjectRef can be safely cast to a JSValueRef.
// https://lists.webkit.org/pipermail/webkit-dev/2009-May/007530.html
return C.JSValueRef(obj.ref)
}
type errorValue struct {
ctx *Context
ref C.JSValueRef
}
func (ctx *Context) newErrorValue() *errorValue {
return &errorValue{ctx, nil}
}
// Error returns a string describing the exception. If r.ref is nil, it panics.
//
// This is because if r.ref is nil, then errorValue is being used improperly.
// It's intended to be used as an argument to functions that take a
// *C.JSValueRef and set the pointer if there is an error. If it's used as such,
// and r.ref is nil, then the function that received this errorValue's
// *C.JSValueRef did not return an error. To determine whether an error
// occurred, the programmer must check whether this errorValue's ref field is
// nil, NOT whether a pointer to this errorValue is nil. This function panics
// instead of returning, say, an empty error string, to prevent this misuse.
func (r errorValue) Error() string {
if r.ref == nil {
panic("errorValue.ref is nil")
}
v := r.ctx.newValue(r.ref)
return v.ToStringOrDie()
}