Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing test for onError handler. #3587

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import './ambient';
export {
onMount,
onDestroy,
onError,
beforeUpdate,
afterUpdate,
setContext,
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/internal/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface T$$ {
context: Map<any, any>;
on_mount: any[];
on_destroy: any[];
on_error: any[];
}

export function bind(component, name, callback) {
Expand Down Expand Up @@ -114,6 +115,7 @@ export function init(component, options, instance, create_fragment, not_equal, p

// lifecycle
on_mount: [],
on_error: [],
on_destroy: [],
before_update: [],
after_update: [],
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/internal/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export function onDestroy(fn) {
get_current_component().$$.on_destroy.push(fn);
}

export function onError(fn) {
get_current_component().$$.on_error.push(fn);
}

export function createEventDispatcher() {
const component = get_current_component();

Expand Down
38 changes: 31 additions & 7 deletions src/runtime/internal/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,36 @@ export function flush() {
}

function update($$) {
if ($$.fragment !== null) {
$$.update($$.dirty);
run_all($$.before_update);
$$.fragment && $$.fragment.p($$.dirty, $$.ctx);
$$.dirty = null;

$$.after_update.forEach(add_render_callback);
if ($$.fragment) {
if ($$.on_error.length == 0) {
exec_update($$);
} else {
try_exec_update($$);
}
}
}

function exec_update($$) {
$$.update($$.dirty);
run_all($$.before_update);
$$.fragment && $$.fragment.p($$.dirty, $$.ctx);
$$.dirty = null;

$$.after_update.forEach(add_render_callback);
}

function try_exec_update($$) {
try {
exec_update($$);
} catch (e) {
let handled = false;
for (let i = 0; i < $$.on_error.length; i += 1) {
const callback = $$.on_error[i];
const result = callback(e);
if (result !== false) {
handled = true;
}
}
if (!handled) throw e;
}
}
1 change: 1 addition & 0 deletions src/runtime/internal/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export function create_ssr_component(fn) {

// these will be immediately discarded
on_mount: [],
on_error: [],
before_update: [],
after_update: [],
callbacks: blank_object()
Expand Down
7 changes: 7 additions & 0 deletions test/runtime/samples/onerror-fires-when-error/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
test({ assert, target }) {
const div = target.querySelector('div');

assert.equal('error', div.className);
}
};
1 change: 1 addition & 0 deletions test/runtime/samples/onerror-fires-when-error/container.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
16 changes: 16 additions & 0 deletions test/runtime/samples/onerror-fires-when-error/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script>
import { onError } from 'svelte';
let error;
onError(err => {
error = err;
});
function getWidget() {
throw new Error('Oops');
}
</script>

{#if error}
<div class='error'>{error.message}</div>
{:else}
<div class='success'>{getWidget()}</div>
{/if}