Skip to content

Commit

Permalink
feat: handle error for promise and ob
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini committed Mar 5, 2024
1 parent a777542 commit 49c86f3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
18 changes: 18 additions & 0 deletions packages/genji-theme-vitepress/__tests__/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,21 @@ display(() => {
return null;
})();
```

## Promise Error

```js | dom
new Promise((resolve) => {
const a = 1;
a = 2;
});
```

## Observable Error

```js | dom
new Observable((observer) => {
const a = 1;
a = 2;
});
```
34 changes: 16 additions & 18 deletions packages/genji-theme-vitepress/src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function unmount(node) {

function renderError(e, { script }) {
const [error] = e.stack.split("\n");
const node = document.createElement("div");
const node = document.createElement("span");
node.classList.add("genji-error");
node.textContent = `${script}: ${error} (Open console for more details.)`;
console.error(`${script}:`, e);
Expand Down Expand Up @@ -144,6 +144,17 @@ function render(module, { isDark }) {

const observable = new Observable((observer) => {
let normalized;

const next = (node) => {
normalized = normalize(node, { isDark });
observer.next(normalized);
};

const error = (e) => {
normalized = renderError(e, { script });
observer.error(normalized);
};

try {
const parsed = parseCode(code, P);

Expand All @@ -153,30 +164,17 @@ function render(module, { isDark }) {
`return value //# sourceURL=${script}`
)
)();
const next = (node) => {
normalized = normalize(node, { isDark });
observer.next(normalized);
};

if (node instanceof Promise) {
next(renderLoading());
node
.then((d) => next(d))
.catch((e) => {
throw e;
});
node.then(next).catch(error);
} else if (node instanceof Observable) {
node.subscribe({
next: (d) => next(d),
error: (e) => {
throw e;
},
});
node.subscribe({ next, error });
} else {
next(node);
}
} catch (e) {
normalized = renderError(e, { script });
observer.error(normalized);
error(e);
} finally {
return () => unmount(normalized);
}
Expand Down

0 comments on commit 49c86f3

Please sign in to comment.