Skip to content

Commit e48aa2f

Browse files
FredKSchottNate Moore
andauthored
add error hints (#3350)
* add error hints * chore: add changeset Co-authored-by: Nate Moore <nate@skypack.dev>
1 parent 8666f22 commit e48aa2f

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

.changeset/dry-glasses-guess.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Improve error hints for packages that should be added to `vite.ssr.noExternal`

packages/astro/src/core/errors.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface ErrorWithMetadata {
99
[name: string]: any;
1010
message: string;
1111
stack: string;
12+
hint?: string;
1213
id?: string;
1314
frame?: string;
1415
plugin?: string;
@@ -40,6 +41,13 @@ export function fixViteErrorMessage(_err: unknown, server: ViteDevServer) {
4041
return err;
4142
}
4243

44+
function generateHint(err: ErrorWithMetadata): string | undefined {
45+
if (/Unknown file extension \"\.(jsx|vue|svelte|astro)\" for /.test(err.message)) {
46+
return 'You likely need to add this package to `vite.ssr.noExternal` in your astro config file.';
47+
}
48+
return undefined;
49+
}
50+
4351
/**
4452
* Takes any error-like object and returns a standardized Error + metadata object.
4553
* Useful for consistent reporting regardless of where the error surfaced from.
@@ -70,9 +78,11 @@ export function collectErrorMetadata(e: any): ErrorWithMetadata {
7078
if (pluginName) {
7179
err.plugin = pluginName;
7280
}
81+
err.hint = generateHint(err);
7382
return err;
7483
}
7584

7685
// Generic error (probably from Vite, and already formatted)
86+
e.hint = generateHint(e);
7787
return e;
7888
}

packages/astro/src/core/messages.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ export function formatConfigErrorMessage(err: ZodError) {
212212
export function formatErrorMessage(_err: Error, args: string[] = []): string {
213213
const err = collectErrorMetadata(_err);
214214
args.push(`${bgRed(black(` error `))}${red(bold(padMultilineString(err.message)))}`);
215+
if (err.hint) {
216+
args.push(` ${bold('Hint:')}`);
217+
args.push(yellow(padMultilineString(err.hint, 4)));
218+
}
215219
if (err.id) {
216220
args.push(` ${bold('File:')}`);
217221
args.push(red(` ${err.id}`));

0 commit comments

Comments
 (0)