suppressed-error-handling-with-await-using.js (3011B)
1 // |jit-test| skip-if: !getBuildConfiguration("explicit-resource-management"); --enable-explicit-resource-management 2 3 load(libdir + "asserts.js"); 4 5 class CustomError extends Error {} 6 7 { 8 const disposed = []; 9 const errorsToThrow = [new Error("test1"), 1, "2", null, undefined, { error: "test2" }, new CustomError("test3")]; 10 let e; 11 async function testSuppressedErrorWithNoOutsideError() { 12 await using x = { 13 [Symbol.asyncDispose]() { 14 disposed.push(1); 15 throw errorsToThrow[0]; 16 } 17 } 18 await using y = { 19 [Symbol.asyncDispose]() { 20 disposed.push(2); 21 throw errorsToThrow[1]; 22 } 23 } 24 using z = { 25 [Symbol.dispose]() { 26 disposed.push(3); 27 throw errorsToThrow[2]; 28 } 29 } 30 await using a = { 31 [Symbol.dispose]() { 32 disposed.push(4); 33 throw errorsToThrow[3]; 34 } 35 } 36 await using b = { 37 [Symbol.asyncDispose]: () => { 38 disposed.push(5); 39 throw errorsToThrow[4]; 40 } 41 } 42 await using c = { 43 async [Symbol.asyncDispose]() { 44 disposed.push(6); 45 throw errorsToThrow[5]; 46 } 47 } 48 await using d = { 49 [Symbol.asyncDispose]: async () => { 50 disposed.push(7); 51 throw errorsToThrow[6]; 52 } 53 } 54 } 55 testSuppressedErrorWithNoOutsideError().catch((err) => { e = err }); 56 drainJobQueue(); 57 assertArrayEq(disposed, [7, 6, 5, 4, 3, 2, 1]); 58 assertSuppressionChain(() => { throw e }, errorsToThrow); 59 } 60 61 { 62 const disposed = []; 63 const errorsToThrow = [new Error("test1"), 1, new CustomError("test2")]; 64 let e; 65 async function testSuppressedErrorWithOutsideError() { 66 await using x = { 67 [Symbol.asyncDispose]() { 68 disposed.push(1); 69 throw errorsToThrow[0]; 70 } 71 } 72 73 await using y = { 74 [Symbol.asyncDispose]() { 75 disposed.push(2); 76 throw errorsToThrow[1]; 77 } 78 } 79 80 throw errorsToThrow[2]; 81 } 82 testSuppressedErrorWithOutsideError().catch((err) => { e = err }); 83 drainJobQueue(); 84 assertArrayEq(disposed, [2, 1]); 85 assertSuppressionChain(() => { throw e }, errorsToThrow); 86 } 87 88 { 89 const disposed = []; 90 let e; 91 async function testSuppressedErrorWithRuntimeErrors() { 92 await using x = { 93 [Symbol.asyncDispose]() { 94 disposed.push(1); 95 undefined.x; 96 } 97 } 98 await using y = { 99 [Symbol.dispose]() { 100 disposed.push(2); 101 a.x; 102 } 103 } 104 await using z = { 105 [Symbol.asyncDispose]() { 106 this[Symbol.asyncDispose](); 107 } 108 } 109 110 null.x; 111 } 112 testSuppressedErrorWithRuntimeErrors().catch((err) => { e = err }); 113 drainJobQueue(); 114 assertArrayEq(disposed, [2, 1]); 115 assertSuppressionChainErrorMessages(() => { throw e }, [ 116 {ctor: TypeError, message: "can't access property \"x\" of undefined"}, 117 {ctor: ReferenceError, message: "a is not defined"}, 118 {ctor: InternalError, message: "too much recursion"}, 119 {ctor: TypeError, message: "can't access property \"x\" of null"}, 120 ]); 121 }