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

Function TestValueError

value_test.go:1308–1357  ·  view source on GitHub ↗

TestValueError tests error handling

(t *testing.T)

Source from the content-addressed store, hash-verified

1306
1307// TestValueError tests error handling
1308func TestValueError(t *testing.T) {
1309 useStableOwnerHooksForLegacySubtests(t)
1310
1311 rt := NewRuntime()
1312 defer rt.Close()
1313 ctx := rt.NewContext()
1314 defer ctx.Close()
1315
1316 // Test error creation and conversion - Updated to use New* methods
1317 testErr := errors.New("test error message")
1318 errorVal := ctx.NewError(testErr)
1319 defer errorVal.Free()
1320
1321 require.True(t, errorVal.IsError())
1322
1323 // Test new method
1324 convertedErr := errorVal.ToError()
1325 require.NotNil(t, convertedErr)
1326 require.Contains(t, convertedErr.Error(), "test error message")
1327
1328 // Test deprecated method
1329 deprecatedErr := errorVal.Error()
1330 require.NotNil(t, deprecatedErr)
1331
1332 // Test ToError on non-error value
1333 str := ctx.NewString("not an error")
1334 defer str.Free()
1335 require.Nil(t, str.ToError())
1336
1337 // Test complex error with all properties - FIXED: removed error handling
1338 complexError := ctx.Eval(`
1339 const err = new Error("complex error");
1340 err.name = "CustomError";
1341 err.cause = "underlying cause";
1342 err.stack = "stack trace here";
1343 err;
1344 `)
1345 defer complexError.Free()
1346 require.False(t, complexError.IsException()) // Check for exceptions instead of error
1347
1348 complexConvertedErr := complexError.ToError()
1349 require.NotNil(t, complexConvertedErr)
1350
1351 quickjsErr, ok := complexConvertedErr.(*Error)
1352 require.True(t, ok)
1353 require.Equal(t, "underlying cause", quickjsErr.Cause)
1354 require.Equal(t, "CustomError", quickjsErr.Name)
1355 require.Equal(t, "complex error", quickjsErr.Message)
1356 require.Equal(t, "stack trace here", quickjsErr.Stack)
1357}
1358
1359// TestValueInstanceof tests instanceof operations
1360func TestValueInstanceof(t *testing.T) {

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
NewContextMethod · 0.95
CloseMethod · 0.95
NewErrorMethod · 0.95
ErrorMethod · 0.95
NewStringMethod · 0.95
EvalMethod · 0.95
NewRuntimeFunction · 0.85
NewMethod · 0.80
IsErrorMethod · 0.80
ToErrorMethod · 0.80

Tested by

no test coverage detected