Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,64 +288,63 @@ func (c *Context) Exception() error {
}

// Throw throws a value as an exception.
func (c *Context) Throw(v *Value) {
val := c.Call("JS_Throw", c.Raw(), v.Raw())
c.Call("JS_FreeValue", c.Raw(), val.Raw())
func (c *Context) Throw(v *Value) *Value {
return c.Call("JS_Throw", c.Raw(), v.Raw())
}

// ThrowError throws an exception with the given error.
func (c *Context) ThrowError(err error) {
c.Throw(c.NewError(err))
func (c *Context) ThrowError(err error) *Value {
return c.Throw(c.NewError(err))
}

// ThrowSyntaxError throws syntax error with given cause.
func (c *Context) ThrowSyntaxError(format string, args ...any) {
func (c *Context) ThrowSyntaxError(format string, args ...any) *Value {
cause := fmt.Sprintf(format, args...)

causePtr := c.NewStringHandle(cause)
defer causePtr.Free()

c.Call("QJS_ThrowSyntaxError", c.Raw(), causePtr.Raw())
return c.Call("QJS_ThrowSyntaxError", c.Raw(), causePtr.Raw())
}

// ThrowTypeError throws type error with given cause.
func (c *Context) ThrowTypeError(format string, args ...any) {
func (c *Context) ThrowTypeError(format string, args ...any) *Value {
cause := fmt.Sprintf(format, args...)

causePtr := c.NewStringHandle(cause)
defer causePtr.Free()

c.Call("QJS_ThrowTypeError", c.Raw(), causePtr.Raw())
return c.Call("QJS_ThrowTypeError", c.Raw(), causePtr.Raw())
}

// ThrowReferenceError throws reference error with given cause.
func (c *Context) ThrowReferenceError(format string, args ...any) {
func (c *Context) ThrowReferenceError(format string, args ...any) *Value {
cause := fmt.Sprintf(format, args...)

causePtr := c.NewStringHandle(cause)
defer causePtr.Free()

c.Call("QJS_ThrowReferenceError", c.Raw(), causePtr.Raw())
return c.Call("QJS_ThrowReferenceError", c.Raw(), causePtr.Raw())
}

// ThrowRangeError throws range error with given cause.
func (c *Context) ThrowRangeError(format string, args ...any) {
func (c *Context) ThrowRangeError(format string, args ...any) *Value {
cause := fmt.Sprintf(format, args...)

causePtr := c.NewStringHandle(cause)
defer causePtr.Free()

c.Call("QJS_ThrowRangeError", c.Raw(), causePtr.Raw())
return c.Call("QJS_ThrowRangeError", c.Raw(), causePtr.Raw())
}

// ThrowInternalError throws internal error with given cause.
func (c *Context) ThrowInternalError(format string, args ...any) {
func (c *Context) ThrowInternalError(format string, args ...any) *Value {
cause := fmt.Sprintf(format, args...)

causePtr := c.NewStringHandle(cause)
defer causePtr.Free()

c.Call("QJS_ThrowInternalError", c.Raw(), causePtr.Raw())
return c.Call("QJS_ThrowInternalError", c.Raw(), causePtr.Raw())
}

// Function creates a JavaScript function that wraps the given Go function.
Expand Down
21 changes: 7 additions & 14 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,48 +222,42 @@ func TestContextErrorHandling(t *testing.T) {
{
name: "throw_error",
throwFunc: func() *qjs.Value {
ctx.ThrowError(errors.New("custom error"))
return nil
return ctx.ThrowError(errors.New("custom error"))
},
expectedErrMsg: "custom error",
},
{
name: "throw_syntax_error",
throwFunc: func() *qjs.Value {
ctx.ThrowSyntaxError("syntax %s", "error")
return nil
return ctx.ThrowSyntaxError("syntax %s", "error")
},
expectedErrMsg: "syntax error",
},
{
name: "throw_type_error",
throwFunc: func() *qjs.Value {
ctx.ThrowTypeError("type %s", "error")
return nil
return ctx.ThrowTypeError("type %s", "error")
},
expectedErrMsg: "type error",
},
{
name: "throw_reference_error",
throwFunc: func() *qjs.Value {
ctx.ThrowReferenceError("reference %s", "error")
return nil
return ctx.ThrowReferenceError("reference %s", "error")
},
expectedErrMsg: "reference error",
},
{
name: "throw_range_error",
throwFunc: func() *qjs.Value {
ctx.ThrowRangeError("range %s", "error")
return nil
return ctx.ThrowRangeError("range %s", "error")
},
expectedErrMsg: "range error",
},
{
name: "throw_internal_error",
throwFunc: func() *qjs.Value {
ctx.ThrowInternalError("internal %s", "error")
return nil
return ctx.ThrowInternalError("internal %s", "error")
},
expectedErrMsg: "internal error",
},
Expand Down Expand Up @@ -477,8 +471,7 @@ func createErrorHandlingTests() []functionTestCase {
name: "throw_custom_error",
expectErrorString: "custom error",
fn: func(this *qjs.This) (*qjs.Value, error) {
this.Context().ThrowError(errors.New("custom error"))
return this.NewUndefined(), nil
return this.Context().ThrowError(errors.New("custom error")), nil
},
},
}
Expand Down
3 changes: 1 addition & 2 deletions functojs.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,8 @@ func GoFuncResultToJs(c *Context, results []reflect.Value) (*Value, error) {
// Last return is non-nil error -> throw in JS context
if !lastResult.IsNil() {
resultErr, _ := lastResult.Interface().(error)
c.ThrowError(resultErr)

return nil, nil
return nil, resultErr
}

// Error is nil, handle remaining return values
Expand Down
11 changes: 4 additions & 7 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ func createFuncProxyWithRegistry(registry *ProxyRegistry) JsFunctionProxy {

result, err := goFunc(this)
if err != nil {
this.context.ThrowError(err)

return this.context.NewUndefined().Raw()
return this.context.ThrowError(err).Raw()
}

return validateAndReturnResult(this, result)
Expand All @@ -149,12 +147,11 @@ func handlePanicRecovery(this *This, r any) uint64 {
if this.isAsync && this.promise != nil {
errVal := this.context.NewError(recoveredErr)
rejectErr := this.promise.Reject(errVal)
this.context.ThrowError(combineErrors(recoveredErr, rejectErr))
} else {
this.context.ThrowError(recoveredErr)

return this.context.ThrowError(combineErrors(recoveredErr, rejectErr)).Raw()
}

return this.context.NewUndefined().Raw()
return this.context.ThrowError(recoveredErr).Raw()
}

// validateAndReturnResult validates the function result and handles JavaScript exceptions.
Expand Down
3 changes: 1 addition & 2 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ func TestProxyErrorHandling(t *testing.T) {
name: "function_throws_error",
setupFn: func(ctx *qjs.Context) {
ctx.SetFunc("throwingFunction", func(this *qjs.This) (*qjs.Value, error) {
this.Context().ThrowError(errors.New("thrown error"))
return this.NewUndefined(), nil
return this.Context().ThrowError(errors.New("thrown error")), nil
})
},
testCode: "throwingFunction()",
Expand Down
Loading