Skip to content

Commit 9d5d95a

Browse files
committed
add missing error handling
1 parent f34bc31 commit 9d5d95a

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

exampleVault/Test.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
text: this is some text
2+
text: this is reactive
33
number: 12234234
44
---
55

@@ -164,6 +164,9 @@ You can also create reactive components that re-render based on a specific event
164164
165165
// define a function that takes in some args and returns what should be rendered
166166
function render(args) {
167+
if (args?.text === "this") {
168+
throw new Error("not allowed");
169+
}
167170
return args?.text ?? "undefined";
168171
}
169172

jsEngine/api/API.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@ export class API {
7979
* @param initialArgs the initial arguments (for the first render) to pass to the function.
8080
*/
8181
public reactive(fn: JsFunc, ...initialArgs: unknown[]): ReactiveComponent {
82-
return new ReactiveComponent(fn, initialArgs);
82+
return new ReactiveComponent(this, fn, initialArgs);
8383
}
8484
}

jsEngine/api/reactive/ReactiveComponent.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { type ResultRenderer } from '../../engine/ResultRenderer';
22
import { type JsFunc } from '../../engine/JsExecution';
3+
import { MessageType } from 'jsEngine/messages/MessageManager';
4+
import { type API } from 'jsEngine/api/API';
35

46
/**
57
* A reactive component is a component that can be refreshed.
@@ -8,14 +10,16 @@ import { type JsFunc } from '../../engine/JsExecution';
810
* See {@link API.reactive}
911
*/
1012
export class ReactiveComponent {
13+
private readonly apiInstance: API;
1114
private readonly _render: JsFunc;
1215
private readonly initialArgs: unknown[];
1316
/**
1417
* @internal
1518
*/
1619
renderer: ResultRenderer | undefined;
1720

18-
constructor(_render: JsFunc, initialArgs: unknown[]) {
21+
constructor(api: API, _render: JsFunc, initialArgs: unknown[]) {
22+
this.apiInstance = api;
1923
this._render = _render;
2024
this.initialArgs = initialArgs;
2125
}
@@ -26,7 +30,25 @@ export class ReactiveComponent {
2630
* @param args
2731
*/
2832
public async refresh(...args: unknown[]): Promise<void> {
29-
void this.renderer?.render(await this._render(...args));
33+
let result: unknown;
34+
35+
try {
36+
// eslint-disable-next-line
37+
result = await this._render(...args);
38+
} catch (e) {
39+
console.warn('failed to execute JS', e);
40+
41+
if (e instanceof Error) {
42+
result = this.apiInstance.message.createMessage(
43+
MessageType.ERROR,
44+
'Failed to execute JS',
45+
`Failed to execute JS during reactive execution in execution "${this.apiInstance.instanceId.id}"`,
46+
e.stack,
47+
);
48+
}
49+
}
50+
51+
void this.renderer?.render(result);
3052
}
3153

3254
/**

0 commit comments

Comments
 (0)