Skip to content

Commit 866ed40

Browse files
authored
feat: expose user astro error (#8012)
1 parent 519a1c4 commit 866ed40

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed

.changeset/wild-bobcats-carry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': minor
3+
---
4+
5+
Add a new `astro/errors` module. Developers can import `AstroUserError`, and provide a `message` and an optional `hint`

packages/astro/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"types": "./zod.d.ts",
7070
"default": "./zod.mjs"
7171
},
72+
"./errors": "./dist/core/errors/userError.js",
7273
"./middleware": {
7374
"types": "./dist/core/middleware/index.d.ts",
7475
"default": "./dist/core/middleware/index.js"

packages/astro/src/core/errors/errors.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface ErrorLocation {
1919

2020
type ErrorTypes =
2121
| 'AstroError'
22+
| 'AstroUserError'
2223
| 'CompilerError'
2324
| 'CSSError'
2425
| 'MarkdownError'
@@ -171,3 +172,25 @@ export interface ErrorWithMetadata {
171172
};
172173
cause?: any;
173174
}
175+
176+
/**
177+
* Special error that is exposed to users.
178+
* Compared to AstroError, it contains a subset of information.
179+
*/
180+
export class AstroUserError extends Error {
181+
type: ErrorTypes = 'AstroUserError';
182+
/**
183+
* A message that explains to the user how they can fix the error.
184+
*/
185+
hint: string | undefined;
186+
name = 'AstroUserError';
187+
constructor(message: string, hint?: string) {
188+
super();
189+
this.message = message;
190+
this.hint = hint;
191+
}
192+
193+
static is(err: unknown): err is AstroUserError {
194+
return (err as AstroUserError).type === 'AstroUserError';
195+
}
196+
}

packages/astro/src/core/errors/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export {
77
CompilerError,
88
MarkdownError,
99
isAstroError,
10+
AstroUserError,
1011
} from './errors.js';
1112
export { codeFrame } from './printer.js';
1213
export { createSafeError, positionAt } from './utils.js';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { AstroUserError as AstroError } from './errors.js';

packages/astro/src/core/messages.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ import {
1717
import type { ResolvedServerUrls } from 'vite';
1818
import type { ZodError } from 'zod';
1919
import { renderErrorMarkdown } from './errors/dev/utils.js';
20-
import { AstroError, CompilerError, type ErrorWithMetadata } from './errors/index.js';
20+
import {
21+
AstroError,
22+
CompilerError,
23+
type ErrorWithMetadata,
24+
AstroUserError,
25+
} from './errors/index.js';
2126
import { emoji, padMultilineString } from './util.js';
2227

2328
const PREFIX_PADDING = 6;
@@ -198,7 +203,7 @@ export function formatConfigErrorMessage(err: ZodError) {
198203
}
199204

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

203208
args.push(
204209
`${bgRed(black(` error `))}${red(

0 commit comments

Comments
 (0)