Skip to content

Commit

Permalink
feat: expose user astro error (#8012)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Aug 11, 2023
1 parent 519a1c4 commit 866ed40
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-bobcats-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

Add a new `astro/errors` module. Developers can import `AstroUserError`, and provide a `message` and an optional `hint`
1 change: 1 addition & 0 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"types": "./zod.d.ts",
"default": "./zod.mjs"
},
"./errors": "./dist/core/errors/userError.js",
"./middleware": {
"types": "./dist/core/middleware/index.d.ts",
"default": "./dist/core/middleware/index.js"
Expand Down
23 changes: 23 additions & 0 deletions packages/astro/src/core/errors/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ErrorLocation {

type ErrorTypes =
| 'AstroError'
| 'AstroUserError'
| 'CompilerError'
| 'CSSError'
| 'MarkdownError'
Expand Down Expand Up @@ -171,3 +172,25 @@ export interface ErrorWithMetadata {
};
cause?: any;
}

/**
* Special error that is exposed to users.
* Compared to AstroError, it contains a subset of information.
*/
export class AstroUserError extends Error {
type: ErrorTypes = 'AstroUserError';
/**
* A message that explains to the user how they can fix the error.
*/
hint: string | undefined;
name = 'AstroUserError';
constructor(message: string, hint?: string) {
super();
this.message = message;
this.hint = hint;
}

static is(err: unknown): err is AstroUserError {
return (err as AstroUserError).type === 'AstroUserError';
}
}
1 change: 1 addition & 0 deletions packages/astro/src/core/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
CompilerError,
MarkdownError,
isAstroError,
AstroUserError,
} from './errors.js';
export { codeFrame } from './printer.js';
export { createSafeError, positionAt } from './utils.js';
1 change: 1 addition & 0 deletions packages/astro/src/core/errors/userError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { AstroUserError as AstroError } from './errors.js';
9 changes: 7 additions & 2 deletions packages/astro/src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import {
import type { ResolvedServerUrls } from 'vite';
import type { ZodError } from 'zod';
import { renderErrorMarkdown } from './errors/dev/utils.js';
import { AstroError, CompilerError, type ErrorWithMetadata } from './errors/index.js';
import {
AstroError,
CompilerError,
type ErrorWithMetadata,
AstroUserError,
} from './errors/index.js';
import { emoji, padMultilineString } from './util.js';

const PREFIX_PADDING = 6;
Expand Down Expand Up @@ -198,7 +203,7 @@ export function formatConfigErrorMessage(err: ZodError) {
}

export function formatErrorMessage(err: ErrorWithMetadata, args: string[] = []): string {
const isOurError = AstroError.is(err) || CompilerError.is(err);
const isOurError = AstroError.is(err) || CompilerError.is(err) || AstroUserError.is(err);

args.push(
`${bgRed(black(` error `))}${red(
Expand Down

0 comments on commit 866ed40

Please sign in to comment.