0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

fix: check before writing to errors (#11566)

* fix: check before writing to errors

* fix: try using try catches

* test: add

* chore: changeset

* nit: test name
This commit is contained in:
Erika 2024-07-29 17:44:59 +02:00 committed by GitHub
parent d27cf6df7b
commit 0dcef3ab17
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 25 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes DomException errors not being handled properly

View file

@ -125,4 +125,10 @@ test.describe('Error display', () => {
const message = (await getErrorOverlayContent(page)).message; const message = (await getErrorOverlayContent(page)).message;
expect(message).toMatch('can only be used in'); 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');
});
}); });

View file

@ -0,0 +1,3 @@
---
await fetch("https://example.com/", {signal: AbortSignal.timeout(5)})
---

View file

@ -26,7 +26,9 @@ export function collectErrorMetadata(e: any, rootFolder?: URL | undefined): Erro
err.forEach((error) => { err.forEach((error) => {
if (e.stack) { if (e.stack) {
const stackInfo = collectInfoFromStacktrace(e); const stackInfo = collectInfoFromStacktrace(e);
error.stack = stripAnsi(stackInfo.stack); try {
error.stack = stripAnsi(stackInfo.stack);
} catch {}
error.loc = stackInfo.loc; error.loc = stackInfo.loc;
error.plugin = stackInfo.plugin; error.plugin = stackInfo.plugin;
error.pluginCode = stackInfo.pluginCode; error.pluginCode = stackInfo.pluginCode;
@ -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, // 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 // but it will be handled and added below, which is already ANSI-free
if (error.message) { 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.
}
} }
}); });
@ -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 // ESBuild can give us a slightly better error message than the one in the error, so let's use it
if (text) { if (text) {
err[i].message = text; try {
err[i].message = text;
} catch {}
} }
if (location) { if (location) {