From 04286eb40a5e98ebd5540f544c98b12dfcfc42b0 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Thu, 10 Aug 2023 14:51:18 +0100 Subject: [PATCH] chore: address feedback --- .changeset/wild-bobcats-carry.md | 2 +- packages/astro/src/core/errors/errors.ts | 23 +++++++++++++++++++++ packages/astro/src/core/errors/userError.ts | 21 +------------------ packages/astro/src/core/messages.ts | 3 ++- 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/.changeset/wild-bobcats-carry.md b/.changeset/wild-bobcats-carry.md index 645af03b46dd9..ef66ca95241b9 100644 --- a/.changeset/wild-bobcats-carry.md +++ b/.changeset/wild-bobcats-carry.md @@ -2,4 +2,4 @@ 'astro': minor --- -Add a new `astro/errors` module. You can import `createAstroError` API to create and throw Astro errors. +Add a new `astro/errors` module. Developers can import `AstroUserError`, and provide a `message` and an optional `hint` 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(