File tree Expand file tree Collapse file tree 6 files changed +38
-2
lines changed
Expand file tree Collapse file tree 6 files changed +38
-2
lines changed Original file line number Diff line number Diff line change 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 `
Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ export interface ErrorLocation {
1919
2020type 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+ }
Original file line number Diff line number Diff line change 77 CompilerError ,
88 MarkdownError ,
99 isAstroError ,
10+ AstroUserError ,
1011} from './errors.js' ;
1112export { codeFrame } from './printer.js' ;
1213export { createSafeError , positionAt } from './utils.js' ;
Original file line number Diff line number Diff line change 1+ export { AstroUserError as AstroError } from './errors.js' ;
Original file line number Diff line number Diff line change @@ -17,7 +17,12 @@ import {
1717import type { ResolvedServerUrls } from 'vite' ;
1818import type { ZodError } from 'zod' ;
1919import { 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' ;
2126import { emoji , padMultilineString } from './util.js' ;
2227
2328const PREFIX_PADDING = 6 ;
@@ -198,7 +203,7 @@ export function formatConfigErrorMessage(err: ZodError) {
198203}
199204
200205export 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 (
You can’t perform that action at this time.
0 commit comments