From e0e434234db527dbce14ef4180f284dd779b6194 Mon Sep 17 00:00:00 2001 From: Irshad PI Date: Mon, 17 Aug 2020 19:22:29 +0530 Subject: [PATCH] Await: re-throw error when there is no catch block and promise is rejected (#5149) --- .../compile/render_dom/wrappers/AwaitBlock.ts | 1 + src/runtime/internal/await_block.ts | 3 ++ .../samples/await-without-catch/_config.js | 44 +++++++++++++++++++ .../samples/await-without-catch/main.svelte | 9 ++++ 4 files changed, 57 insertions(+) create mode 100644 test/runtime/samples/await-without-catch/_config.js create mode 100644 test/runtime/samples/await-without-catch/main.svelte diff --git a/src/compiler/compile/render_dom/wrappers/AwaitBlock.ts b/src/compiler/compile/render_dom/wrappers/AwaitBlock.ts index 495f1b3157..e890715692 100644 --- a/src/compiler/compile/render_dom/wrappers/AwaitBlock.ts +++ b/src/compiler/compile/render_dom/wrappers/AwaitBlock.ts @@ -188,6 +188,7 @@ export default class AwaitBlockWrapper extends Wrapper { ctx: #ctx, current: null, token: null, + hasCatch: ${this.catch.node.start !== null ? 'true' : 'false'}, pending: ${this.pending.block.name}, then: ${this.then.block.name}, catch: ${this.catch.block.name}, diff --git a/src/runtime/internal/await_block.ts b/src/runtime/internal/await_block.ts index f70cbd6c2c..3821520837 100644 --- a/src/runtime/internal/await_block.ts +++ b/src/runtime/internal/await_block.ts @@ -59,6 +59,9 @@ export function handle_promise(promise, info) { update(info.then, 1, info.value, value); set_current_component(null); }, error => { + if (!info.hasCatch) { + throw error; + } set_current_component(current_component); update(info.catch, 2, info.error, error); set_current_component(null); diff --git a/test/runtime/samples/await-without-catch/_config.js b/test/runtime/samples/await-without-catch/_config.js new file mode 100644 index 0000000000..2030ed7949 --- /dev/null +++ b/test/runtime/samples/await-without-catch/_config.js @@ -0,0 +1,44 @@ +let fulfil; + +let promise = new Promise(f => { + fulfil = f; +}); + +export default { + props: { + promise + }, + + html: ` +

loading...

+ `, + + test({ assert, component, target }) { + fulfil(42); + + return promise + .then(() => { + assert.htmlEqual(target.innerHTML, ` +

loaded

+ `); + + let reject; + + promise = new Promise((f, r) => { + reject = r; + }); + + component.promise = promise; + + assert.htmlEqual(target.innerHTML, ` +

loading...

+ `); + + reject(new Error('this error should be thrown')); + return promise; + }) + .catch((err) => { + assert.equal(err.message, 'this error should be thrown'); + }); + } +}; \ No newline at end of file diff --git a/test/runtime/samples/await-without-catch/main.svelte b/test/runtime/samples/await-without-catch/main.svelte new file mode 100644 index 0000000000..f528a8bf69 --- /dev/null +++ b/test/runtime/samples/await-without-catch/main.svelte @@ -0,0 +1,9 @@ + + +{#await promise} +

loading...

+{:then value} +

loaded

+{/await} \ No newline at end of file