From e26f6c7595f213b149650513fe3f16bd260fdda8 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Thu, 10 Aug 2023 14:51:18 +0100 Subject: [PATCH] chore: address feedback --- packages/astro/src/core/errors/errors.ts | 23 +++++++++++++++++++++ packages/astro/src/core/errors/userError.ts | 21 +------------------ packages/astro/src/core/messages.ts | 3 ++- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/packages/astro/src/core/errors/errors.ts b/packages/astro/src/core/errors/errors.ts index 1960bac4a85ef..faf365686908b 100644 --- a/packages/astro/src/core/errors/errors.ts +++ b/packages/astro/src/core/errors/errors.ts @@ -19,6 +19,7 @@ export interface ErrorLocation { type ErrorTypes = | 'AstroError' + | 'AstroUserError' | 'CompilerError' | 'CSSError' | 'MarkdownError' @@ -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'; + } +} diff --git a/packages/astro/src/core/errors/userError.ts b/packages/astro/src/core/errors/userError.ts index 6a82871a3f894..f347d9c87d3d4 100644 --- a/packages/astro/src/core/errors/userError.ts +++ b/packages/astro/src/core/errors/userError.ts @@ -1,20 +1 @@ -import { AstroError } from './errors.js'; - -export interface CreateAstroError { - /** - * The cause of the error. - */ - message: string; - /** - * An optional message that explain the user how they could fix the error. - */ - hint?: string; -} - -export function createAstroError({ message, hint }: CreateAstroError): AstroError { - return new AstroError({ - name: 'AstroError', - message, - hint, - }); -} +export { AstroUserError } from './errors.js'; diff --git a/packages/astro/src/core/messages.ts b/packages/astro/src/core/messages.ts index 51ec39ad92891..ee4bdc34f5c79 100644 --- a/packages/astro/src/core/messages.ts +++ b/packages/astro/src/core/messages.ts @@ -19,6 +19,7 @@ import type { ZodError } from 'zod'; import { renderErrorMarkdown } from './errors/dev/utils.js'; import { AstroError, CompilerError, type ErrorWithMetadata } from './errors/index.js'; import { emoji, padMultilineString } from './util.js'; +import { AstroUserError } from './errors/errors'; const PREFIX_PADDING = 6; @@ -198,7 +199,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(