From 43e6dd535d577fa90342981dedf21d48baeba94d Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Wed, 9 Aug 2023 09:42:28 +0100 Subject: [PATCH 1/3] feat: expose user astro error --- .changeset/wild-bobcats-carry.md | 5 +++++ packages/astro/package.json | 1 + packages/astro/src/core/errors/userError.ts | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 .changeset/wild-bobcats-carry.md create mode 100644 packages/astro/src/core/errors/userError.ts diff --git a/.changeset/wild-bobcats-carry.md b/.changeset/wild-bobcats-carry.md new file mode 100644 index 000000000000..645af03b46dd --- /dev/null +++ b/.changeset/wild-bobcats-carry.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +Add a new `astro/errors` module. You can import `createAstroError` API to create and throw Astro errors. diff --git a/packages/astro/package.json b/packages/astro/package.json index 8e505e8b0991..c52c93ea3d47 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -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" diff --git a/packages/astro/src/core/errors/userError.ts b/packages/astro/src/core/errors/userError.ts new file mode 100644 index 000000000000..6a82871a3f89 --- /dev/null +++ b/packages/astro/src/core/errors/userError.ts @@ -0,0 +1,20 @@ +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, + }); +} From 14194843e518dac2280ed76f17e05d9b76ab8db1 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Thu, 10 Aug 2023 14:51:18 +0100 Subject: [PATCH 2/3] 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 645af03b46dd..ef66ca95241b 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 1960bac4a85e..faf365686908 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 6a82871a3f89..f347d9c87d3d 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 51ec39ad9289..ee4bdc34f5c7 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( From fa56fbb0eabebf2fcd4ed725b6101544e400102b Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Fri, 11 Aug 2023 15:46:46 +0100 Subject: [PATCH 3/3] chore: alias error --- packages/astro/src/core/errors/index.ts | 1 + packages/astro/src/core/errors/userError.ts | 2 +- packages/astro/src/core/messages.ts | 8 ++++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/astro/src/core/errors/index.ts b/packages/astro/src/core/errors/index.ts index e09225af4237..5a796a0b3fff 100644 --- a/packages/astro/src/core/errors/index.ts +++ b/packages/astro/src/core/errors/index.ts @@ -7,6 +7,7 @@ export { CompilerError, MarkdownError, isAstroError, + AstroUserError, } from './errors.js'; export { codeFrame } from './printer.js'; export { createSafeError, positionAt } from './utils.js'; diff --git a/packages/astro/src/core/errors/userError.ts b/packages/astro/src/core/errors/userError.ts index f347d9c87d3d..6635493145f5 100644 --- a/packages/astro/src/core/errors/userError.ts +++ b/packages/astro/src/core/errors/userError.ts @@ -1 +1 @@ -export { AstroUserError } from './errors.js'; +export { AstroUserError as AstroError } from './errors.js'; diff --git a/packages/astro/src/core/messages.ts b/packages/astro/src/core/messages.ts index ee4bdc34f5c7..4fc3ca02a46e 100644 --- a/packages/astro/src/core/messages.ts +++ b/packages/astro/src/core/messages.ts @@ -17,9 +17,13 @@ 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'; -import { AstroUserError } from './errors/errors'; const PREFIX_PADDING = 6;