MCPcopy Create free account
hub / github.com/buke/quickjs-go / TestContextErrorHandling

Function TestContextErrorHandling

context_test.go:812–868  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

810}
811
812func TestContextErrorHandling(t *testing.T) {
813 useStableOwnerHooksForLegacySubtests(t)
814
815 rt := NewRuntime()
816 defer rt.Close()
817 ctx := rt.NewContext()
818 defer ctx.Close()
819
820 t.Run("ErrorCreation", func(t *testing.T) {
821 testErr := errors.New("test error message")
822 errorVal := ctx.NewError(testErr)
823 defer errorVal.Free()
824 require.True(t, errorVal.IsError())
825 })
826
827 t.Run("ThrowMethods", func(t *testing.T) {
828 throwTests := []struct {
829 name string
830 throwFn func() *Value // Changed to return pointer
831 errorStr string
832 }{
833 {"Throw", func() *Value { return ctx.Throw(ctx.NewError(errors.New("custom throw"))) }, "custom throw"},
834 {"ThrowError", func() *Value { return ctx.ThrowError(errors.New("custom error")) }, "custom error"},
835 {"ThrowSyntax", func() *Value { return ctx.ThrowSyntaxError("syntax: %s", "invalid") }, "SyntaxError"},
836 {"ThrowType", func() *Value { return ctx.ThrowTypeError("type error") }, "TypeError"},
837 {"ThrowReference", func() *Value { return ctx.ThrowReferenceError("ref error") }, "ReferenceError"},
838 {"ThrowRange", func() *Value { return ctx.ThrowRangeError("range error") }, "RangeError"},
839 {"ThrowInternal", func() *Value { return ctx.ThrowInternalError("internal error") }, "InternalError"},
840 }
841
842 for _, tt := range throwTests {
843 t.Run(tt.name, func(t *testing.T) {
844 throwingFunc := ctx.NewFunction(func(ctx *Context, this *Value, args []*Value) *Value {
845 return tt.throwFn()
846 })
847 defer throwingFunc.Free()
848
849 result := throwingFunc.Execute(ctx.NewNull())
850 defer result.Free()
851 require.True(t, result.IsException())
852 require.True(t, ctx.HasException())
853
854 exception := ctx.Exception()
855 require.NotNil(t, exception)
856 require.Contains(t, exception.Error(), tt.errorStr)
857 require.False(t, ctx.HasException()) // Should be cleared
858 })
859 }
860 })
861
862 t.Run("ExceptionHandling", func(t *testing.T) {
863 // Test Exception() when no exception
864 exception := ctx.Exception()
865 require.Nil(t, exception)
866 require.False(t, ctx.HasException())
867 })
868}
869

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
NewContextMethod · 0.95
CloseMethod · 0.95
NewErrorMethod · 0.95
ThrowMethod · 0.95
ThrowErrorMethod · 0.95
ThrowSyntaxErrorMethod · 0.95
ThrowTypeErrorMethod · 0.95
ThrowReferenceErrorMethod · 0.95
ThrowRangeErrorMethod · 0.95
ThrowInternalErrorMethod · 0.95
NewFunctionMethod · 0.95

Tested by

no test coverage detected