Skip to content

Commit b85787c

Browse files
authored
chore: allow REPL users to react to compiler/runtime errors (#1518)
1 parent aad941b commit b85787c

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

packages/repl/src/lib/Output/Viewer.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
error = null;
271271
}
272272
} catch (e) {
273+
console.error(e); // make it show up in the console, too, not just the UI
273274
// @ts-ignore
274275
show_error(e);
275276
}

packages/repl/src/lib/Repl.svelte

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
onversion?: (version: string) => void;
2626
onchange?: () => void;
2727
download?: () => void;
28+
/**
29+
* Invoked whenever there's a bundler or runtime error
30+
*/
31+
onerror?: (error: Error) => void;
2832
}
2933
3034
let {
@@ -40,7 +44,8 @@
4044
previewTheme = 'light',
4145
onversion,
4246
onchange = () => {},
43-
download
47+
download,
48+
onerror
4449
}: Props = $props();
4550
4651
// TODO pass in real data
@@ -185,6 +190,18 @@
185190
$toggleable = mobile && orientation === 'columns' && embedded !== 'output-only';
186191
});
187192
193+
$effect(() => {
194+
if (runtime_error) {
195+
onerror?.(runtime_error);
196+
}
197+
});
198+
199+
$effect(() => {
200+
if (bundler.result?.error) {
201+
onerror?.(bundler.result.error as Error);
202+
}
203+
});
204+
188205
let runes = $derived(
189206
workspace.current.name.endsWith('.svelte.js') ||
190207
(workspace.current_compiled?.result?.metadata.runes ?? false)
@@ -235,7 +252,7 @@
235252
{injectedCSS}
236253
{previewTheme}
237254
{workspace}
238-
runtimeError={runtime_error}
255+
bind:runtimeError={runtime_error}
239256
/>
240257
</section>
241258
{/snippet}

packages/repl/src/lib/public.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { OutputChunk, RollupError } from '@rollup/browser';
2+
import type { CompileError } from 'svelte/compiler';
23

34
export interface BundleResult {
45
uid: number;

packages/repl/src/routes/v0.svelte

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@
1313
</script>
1414

1515
<main>
16-
<Repl bind:this={repl} embedded="output-only" />
16+
<Repl
17+
bind:this={repl}
18+
embedded="output-only"
19+
onerror={(error) => {
20+
// @ts-expect-error so that v0 can react to REPL errors
21+
window.__svelte_repl_onerror?.(error);
22+
}}
23+
/>
1724
</main>
1825

1926
<style>

0 commit comments

Comments
 (0)