|
| 1 | +// META: title=globalThis.onerror: runtime script errors in ShadowRealm |
| 2 | + |
| 3 | +// https://html.spec.whatwg.org/multipage/#runtime-script-errors says what to do |
| 4 | +// for uncaught runtime script errors, and just below describes what to do when |
| 5 | +// onerror is a Function. |
| 6 | + |
| 7 | +async_test(t => { |
| 8 | + onerror = t.unreached_func("Window onerror should not be triggered"); |
| 9 | + |
| 10 | + const realm = new ShadowRealm(); |
| 11 | + |
| 12 | + realm.evaluate("var errorCount = 0;"); |
| 13 | + realm.evaluate(`(doAsserts) => { |
| 14 | + globalThis.onerror = function(msg, url, lineno, colno, thrownValue) { |
| 15 | + ++errorCount; |
| 16 | + doAsserts(url, lineno, colno, typeof thrownValue, String(thrownValue)); |
| 17 | + }; |
| 18 | + }`)(t.step_func((url, lineno, typeofThrownValue, stringifiedThrownValue) => { |
| 19 | + assert_equals(url, "eval code", "correct url passed to onerror"); |
| 20 | + assert_equals(lineno, 8, "correct line number passed to onerror"); |
| 21 | + assert_equals(typeofThrownValue, "string", "thrown string passed directly to onerror"); |
| 22 | + assert_equals(stringifiedThrownValue, "bar", "correct thrown value passed to onerror"); |
| 23 | + })); |
| 24 | + |
| 25 | + assert_throws_js(TypeError, () => realm.evaluate(` |
| 26 | + try { |
| 27 | + // This error is caught, so it should NOT trigger onerror. |
| 28 | + throw "foo"; |
| 29 | + } catch (ex) { |
| 30 | + } |
| 31 | + // This error is NOT caught, so it should trigger onerror. |
| 32 | + throw "bar"; |
| 33 | + `), "thrown error is wrapped in a TypeError object from the surrounding realm"); |
| 34 | + |
| 35 | + t.step_timeout(() => { |
| 36 | + assert_equals(realm.evaluate("errorCount"), 1, "onerror should be called once"); |
| 37 | + }, 1000); |
| 38 | +}, "onerror triggered by uncaught thrown exception in realm.evaluate"); |
| 39 | + |
| 40 | +async_test(t => { |
| 41 | + onerror = t.unreached_func("Window onerror should not be triggered"); |
| 42 | + |
| 43 | + const realm = new ShadowRealm(); |
| 44 | + |
| 45 | + realm.evaluate("var errorCount = 0;"); |
| 46 | + realm.evaluate(`(doAsserts) => { |
| 47 | + globalThis.onerror = function(msg, url, lineno, colno, thrownValue) { |
| 48 | + ++errorCount; |
| 49 | + doAsserts(url, lineno, typeof thrownValue, thrownValue instanceof TypeError); |
| 50 | + }; |
| 51 | + }`)(t.step_func((url, lineno, typeofThrownValue, isTypeError) => { |
| 52 | + assert_equals(url, "eval code", "correct url passed to onerror"); |
| 53 | + assert_equals(lineno, 8, "correct line number passed to onerror"); |
| 54 | + assert_equals(typeofThrownValue, "object", "thrown error instance passed to onerror"); |
| 55 | + assert_true(isShadowRealmTypeError, "correct thrown value passed to onerror"); |
| 56 | + })); |
| 57 | + |
| 58 | + assert_throws_js(TypeError, () => realm.evaluate(` |
| 59 | + try { |
| 60 | + // This error is caught, so it should NOT trigger onerror. |
| 61 | + window.nonexistentproperty.oops(); |
| 62 | + } catch (ex) { |
| 63 | + } |
| 64 | + // This error is NOT caught, so it should trigger onerror. |
| 65 | + window.nonexistentproperty.oops(); |
| 66 | + `), "thrown error is wrapped in a TypeError object from the surrounding realm"); |
| 67 | + |
| 68 | + t.step_timeout(() => { |
| 69 | + assert_equals(realm.evaluate("errorCount"), 1, "onerror should be called once"); |
| 70 | + }, 1000); |
| 71 | +}, "onerror triggered by uncaught runtime error in realm.evaluate"); |
0 commit comments