Skip to content

Commit

Permalink
add error hints (#3350)
Browse files Browse the repository at this point in the history
* add error hints

* chore: add changeset

Co-authored-by: Nate Moore <nate@skypack.dev>
  • Loading branch information
FredKSchott and Nate Moore authored May 12, 2022
1 parent 8666f22 commit e48aa2f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-glasses-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Improve error hints for packages that should be added to `vite.ssr.noExternal`
10 changes: 10 additions & 0 deletions packages/astro/src/core/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface ErrorWithMetadata {
[name: string]: any;
message: string;
stack: string;
hint?: string;
id?: string;
frame?: string;
plugin?: string;
Expand Down Expand Up @@ -40,6 +41,13 @@ export function fixViteErrorMessage(_err: unknown, server: ViteDevServer) {
return err;
}

function generateHint(err: ErrorWithMetadata): string | undefined {
if (/Unknown file extension \"\.(jsx|vue|svelte|astro)\" for /.test(err.message)) {
return 'You likely need to add this package to `vite.ssr.noExternal` in your astro config file.';
}
return undefined;
}

/**
* Takes any error-like object and returns a standardized Error + metadata object.
* Useful for consistent reporting regardless of where the error surfaced from.
Expand Down Expand Up @@ -70,9 +78,11 @@ export function collectErrorMetadata(e: any): ErrorWithMetadata {
if (pluginName) {
err.plugin = pluginName;
}
err.hint = generateHint(err);
return err;
}

// Generic error (probably from Vite, and already formatted)
e.hint = generateHint(e);
return e;
}
4 changes: 4 additions & 0 deletions packages/astro/src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ export function formatConfigErrorMessage(err: ZodError) {
export function formatErrorMessage(_err: Error, args: string[] = []): string {
const err = collectErrorMetadata(_err);
args.push(`${bgRed(black(` error `))}${red(bold(padMultilineString(err.message)))}`);
if (err.hint) {
args.push(` ${bold('Hint:')}`);
args.push(yellow(padMultilineString(err.hint, 4)));
}
if (err.id) {
args.push(` ${bold('File:')}`);
args.push(red(` ${err.id}`));
Expand Down

0 comments on commit e48aa2f

Please sign in to comment.