Skip to content

Commit

Permalink
fix: check before writing to errors (#11566)
Browse files Browse the repository at this point in the history
* fix: check before writing to errors

* fix: try using try catches

* test: add

* chore: changeset

* nit: test name
  • Loading branch information
Princesseuh authored Jul 29, 2024
1 parent d27cf6d commit 0dcef3a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-drinks-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes DomException errors not being handled properly
6 changes: 6 additions & 0 deletions packages/astro/e2e/errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,10 @@ test.describe('Error display', () => {
const message = (await getErrorOverlayContent(page)).message;
expect(message).toMatch('can only be used in');
});

test('can handle DomException errors', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/dom-exception'), { waitUntil: 'networkidle' });
const message = (await getErrorOverlayContent(page)).message;
expect(message).toMatch('The operation was aborted due to timeout');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
await fetch("https://example.com/", {signal: AbortSignal.timeout(5)})
---
14 changes: 11 additions & 3 deletions packages/astro/src/core/errors/dev/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export function collectErrorMetadata(e: any, rootFolder?: URL | undefined): Erro
err.forEach((error) => {
if (e.stack) {
const stackInfo = collectInfoFromStacktrace(e);
error.stack = stripAnsi(stackInfo.stack);
try {
error.stack = stripAnsi(stackInfo.stack);
} catch {}
error.loc = stackInfo.loc;
error.plugin = stackInfo.plugin;
error.pluginCode = stackInfo.pluginCode;
Expand Down Expand Up @@ -72,7 +74,11 @@ export function collectErrorMetadata(e: any, rootFolder?: URL | undefined): Erro
// Strip ANSI for `message` property. Note that ESBuild errors may not have the property,
// but it will be handled and added below, which is already ANSI-free
if (error.message) {
error.message = stripAnsi(error.message);
try {
error.message = stripAnsi(error.message);
} catch {
// Setting `error.message` can fail here if the message is read-only, which for the vast majority of cases will never happen, however some somewhat obscure cases can cause this to happen.
}
}
});

Expand All @@ -84,7 +90,9 @@ export function collectErrorMetadata(e: any, rootFolder?: URL | undefined): Erro

// ESBuild can give us a slightly better error message than the one in the error, so let's use it
if (text) {
err[i].message = text;
try {
err[i].message = text;
} catch {}
}

if (location) {
Expand Down

0 comments on commit 0dcef3a

Please sign in to comment.