Initial draft for error handling using `onError`

pull/6585/head
rster20002 5 years ago
parent ff6ce725bf
commit 38c3dab9ba

@ -461,6 +461,10 @@ export default class Block {
this.add_variable(dispose); this.add_variable(dispose);
this.event_listeners.forEach(event_listener => {
event_listener.arguments[2] = x`@attach_error_handler.call(${event_listener.arguments[0]}, #component, ${event_listener.arguments[2]})`;
});
if (this.event_listeners.length === 1) { if (this.event_listeners.length === 1) {
this.chunks.mount.push( this.chunks.mount.push(
b` b`

@ -329,7 +329,7 @@ export default function dom(
const has_create_fragment = component.compile_options.dev || block.has_content(); const has_create_fragment = component.compile_options.dev || block.has_content();
if (has_create_fragment) { if (has_create_fragment) {
body.push(b` body.push(b`
function create_fragment(#ctx) { function create_fragment(#ctx, #component) {
${block.get_contents()} ${block.get_contents()}
} }
`); `);
@ -450,6 +450,15 @@ export default function dom(
}) as Expression) }) as Expression)
}; };
const instance_try_block: any = b`
try {}
catch(e) {
@handle_error($$self, e);
}
`;
instance_try_block[0].block.body = instance_javascript.flat();
body.push(b` body.push(b`
function ${definition}(${args}) { function ${definition}(${args}) {
${injected.map(name => b`let ${name};`)} ${injected.map(name => b`let ${name};`)}
@ -466,7 +475,7 @@ export default function dom(
${component.compile_options.dev && b`@validate_slots('${component.tag}', #slots, [${[...component.slots.keys()].map(key => `'${key}'`).join(',')}]);`} ${component.compile_options.dev && b`@validate_slots('${component.tag}', #slots, [${[...component.slots.keys()].map(key => `'${key}'`).join(',')}]);`}
${compute_slots} ${compute_slots}
${instance_javascript} ${instance_try_block}
${unknown_props_check} ${unknown_props_check}

@ -3,6 +3,7 @@ import './ambient';
export { export {
onMount, onMount,
onDestroy, onDestroy,
onError,
beforeUpdate, beforeUpdate,
afterUpdate, afterUpdate,
setContext, setContext,

@ -36,6 +36,7 @@ interface T$$ {
context: Map<any, any>; context: Map<any, any>;
on_mount: any[]; on_mount: any[];
on_destroy: any[]; on_destroy: any[];
on_error: any[];
skip_bound: boolean; skip_bound: boolean;
on_disconnect: any[]; on_disconnect: any[];
root:Element|ShadowRoot root:Element|ShadowRoot
@ -104,6 +105,26 @@ function make_dirty(component, i) {
component.$$.dirty[(i / 31) | 0] |= (1 << (i % 31)); component.$$.dirty[(i / 31) | 0] |= (1 << (i % 31));
} }
export function attach_error_handler(component, fn) {
return () => {
try {
fn.call(this, ...arguments);
} catch (e) {
handle_error(component, e);
}
}
}
export function handle_error(component, e) {
if (component.$$.on_error.length === 0) {
throw e;
}
component.$$.on_error.forEach(handler => {
handler(e);
});
}
export function init(component, options, instance, create_fragment, not_equal, props, append_styles, dirty = [-1]) { export function init(component, options, instance, create_fragment, not_equal, props, append_styles, dirty = [-1]) {
const parent_component = current_component; const parent_component = current_component;
set_current_component(component); set_current_component(component);
@ -122,6 +143,7 @@ export function init(component, options, instance, create_fragment, not_equal, p
on_mount: [], on_mount: [],
on_destroy: [], on_destroy: [],
on_disconnect: [], on_disconnect: [],
on_error: [],
before_update: [], before_update: [],
after_update: [], after_update: [],
context: new Map(parent_component ? parent_component.$$.context : options.context || []), context: new Map(parent_component ? parent_component.$$.context : options.context || []),
@ -153,7 +175,7 @@ export function init(component, options, instance, create_fragment, not_equal, p
run_all($$.before_update); run_all($$.before_update);
// `false` as a special case of no DOM component // `false` as a special case of no DOM component
$$.fragment = create_fragment ? create_fragment($$.ctx) : false; $$.fragment = create_fragment ? create_fragment($$.ctx, component) : false;
if (options.target) { if (options.target) {
if (options.hydrate) { if (options.hydrate) {

@ -27,6 +27,10 @@ export function onDestroy(fn: () => any) {
get_current_component().$$.on_destroy.push(fn); get_current_component().$$.on_destroy.push(fn);
} }
export function onError(fn: () => any) {
get_current_component().$$.on_error.push(fn);
}
export function createEventDispatcher< export function createEventDispatcher<
EventMap extends {} = any EventMap extends {} = any
>(): <EventKey extends Extract<keyof EventMap, string>>(type: EventKey, detail?: EventMap[EventKey]) => void { >(): <EventKey extends Extract<keyof EventMap, string>>(type: EventKey, detail?: EventMap[EventKey]) => void {

Loading…
Cancel
Save