diff --git a/src/compiler/compile/render_dom/Block.ts b/src/compiler/compile/render_dom/Block.ts
index 3d149275e7..7e3249a31e 100644
--- a/src/compiler/compile/render_dom/Block.ts
+++ b/src/compiler/compile/render_dom/Block.ts
@@ -316,7 +316,12 @@ export default class Block {
properties.update = x`function #update(${ctx}, ${dirty}) {
${this.maintain_context && b`#ctx = ${ctx};`}
- ${this.chunks.update}
+
+ try {
+ ${this.chunks.update}
+ } catch (e) {
+ @handle_error(@get_current_component(), e);
+ }
}`;
}
}
diff --git a/src/compiler/compile/render_dom/index.ts b/src/compiler/compile/render_dom/index.ts
index 68bff74f90..cebc42ecd0 100644
--- a/src/compiler/compile/render_dom/index.ts
+++ b/src/compiler/compile/render_dom/index.ts
@@ -6,7 +6,7 @@ import { walk } from 'estree-walker';
import { extract_names, Scope } from 'periscopic';
import { invalidate } from './invalidate';
import Block from './Block';
-import { ImportDeclaration, ClassDeclaration, FunctionExpression, Node, Statement, ObjectExpression, Expression } from 'estree';
+import { ImportDeclaration, ClassDeclaration, FunctionExpression, Node, Statement, ObjectExpression, Expression, Identifier } from 'estree';
import { apply_preprocessor_sourcemap } from '../../utils/mapped_code';
import { RawSourceMap, DecodedSourceMap } from '@ampproject/remapping/dist/types/types';
import { flatten } from '../../utils/flatten';
@@ -442,7 +442,7 @@ export default function dom(
`;
}
- const return_value_reference = b`let return_values = []`;
+ const return_value_reference = b`let #return_values = []`;
const return_value = {
type: 'ArrayExpression',
@@ -451,10 +451,42 @@ export default function dom(
name: member.name
}) as Expression)
};
+
+ let instance_javascript_with_ctx = [];
+ let initializedIdentifiers = [];
+ instance_javascript.forEach(node => {
+ instance_javascript_with_ctx.push(node);
+
+ if (Array.isArray(node) && node[0].type === "VariableDeclaration" ) {
+ walk(node[0], {
+ enter(declaration: Identifier) {
+ if (declaration.type === 'Identifier' && !initializedIdentifiers.includes(declaration.name)) {
+ let index = renderer.initial_context.findIndex(member => member.name === declaration.name);
+
+ if (index >= 0) {
+ node.push(x`#return_values[${index}] = ${declaration}`);
+ initializedIdentifiers.push(declaration.name);
+ }
+ }
+ }
+ });
+ }
+
+ if (node.type === "FunctionDeclaration") {
+ if (!initializedIdentifiers.includes(node.id.name)) {
+ let index = renderer.initial_context.findIndex(member => member.name === node.id.name);
+
+ if (index >= 0) {
+ instance_javascript_with_ctx.push(x`#return_values[${index}] = ${node.id.name}`);
+ initializedIdentifiers.push(node.id.name);
+ }
+ }
+ }
+ });
const instance_try_block: any = b`
try {
- ${instance_javascript}
+ ${instance_javascript_with_ctx}
${unknown_props_check}
@@ -480,10 +512,13 @@ export default function dom(
${uses_props && b`$$props = @exclude_internal_props($$props);`}
- return ${return_value}
+ #return_values = ${return_value}
+ return #return_values;
}
catch(e) {
+ $$self.$$.ctx = #return_values;
@handle_error($$self, e);
+ return #return_values;
}
`;
diff --git a/src/compiler/compile/render_ssr/index.ts b/src/compiler/compile/render_ssr/index.ts
index b35a6ce6ff..e352b610f9 100644
--- a/src/compiler/compile/render_ssr/index.ts
+++ b/src/compiler/compile/render_ssr/index.ts
@@ -186,7 +186,11 @@ export default function ssr(
${reactive_store_unsubscriptions}
- return ${literal};`;
+ try {
+ return ${literal};
+ } catch (e) {
+ @handle_error(@get_current_component(), e);
+ }`;
const blocks = [
...injected.map(name => b`let ${name};`),
@@ -194,7 +198,13 @@ export default function ssr(
slots,
...reactive_store_declarations,
...reactive_store_subscriptions,
- instance_javascript,
+ b`
+ try {
+ ${instance_javascript}
+ } catch (e) {
+ @handle_error(@get_current_component(), e);
+ }
+ `,
...parent_bindings,
css.code && b`$$result.css.add(#css);`,
main
diff --git a/src/runtime/internal/ssr.ts b/src/runtime/internal/ssr.ts
index c64d88fa75..f7d3616d02 100644
--- a/src/runtime/internal/ssr.ts
+++ b/src/runtime/internal/ssr.ts
@@ -97,6 +97,8 @@ export function create_ssr_component(fn) {
on_mount: [],
before_update: [],
after_update: [],
+ on_error: [],
+ parent_component,
callbacks: blank_object()
};
diff --git a/test/runtime/samples/error-handling-bubble-to-parent-manually/main.svelte b/test/runtime/samples/error-handling-bubble-to-parent-manually/main.svelte
index 5c7caa7dc9..8a7621bd24 100644
--- a/test/runtime/samples/error-handling-bubble-to-parent-manually/main.svelte
+++ b/test/runtime/samples/error-handling-bubble-to-parent-manually/main.svelte
@@ -1,9 +1,12 @@
\ No newline at end of file
+
+
+
\ No newline at end of file
diff --git a/test/runtime/samples/error-handling-bubble-to-parent/main.svelte b/test/runtime/samples/error-handling-bubble-to-parent/main.svelte
index 5c7caa7dc9..8a7621bd24 100644
--- a/test/runtime/samples/error-handling-bubble-to-parent/main.svelte
+++ b/test/runtime/samples/error-handling-bubble-to-parent/main.svelte
@@ -1,9 +1,12 @@
\ No newline at end of file
+
+
+
\ No newline at end of file
diff --git a/test/runtime/samples/error-handling-event-correct-this/main.svelte b/test/runtime/samples/error-handling-event-correct-this/main.svelte
index 89fe5e86a4..a8760ae348 100644
--- a/test/runtime/samples/error-handling-event-correct-this/main.svelte
+++ b/test/runtime/samples/error-handling-event-correct-this/main.svelte
@@ -2,7 +2,7 @@
import { onError } from "svelte";
var a = {};
- var that;
+ export var that;
export var error = false;
onError(e => {