Skip to content

Commit

Permalink
chore: address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Aug 10, 2023
1 parent 0a1a550 commit 04286eb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .changeset/wild-bobcats-carry.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
23 changes: 23 additions & 0 deletions packages/astro/src/core/errors/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ErrorLocation {

type ErrorTypes =
| 'AstroError'
| 'AstroUserError'
| 'CompilerError'
| 'CSSError'
| 'MarkdownError'
Expand Down Expand Up @@ -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';
}
}
21 changes: 1 addition & 20 deletions packages/astro/src/core/errors/userError.ts
Original file line number Diff line number Diff line change
@@ -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';
3 changes: 2 additions & 1 deletion packages/astro/src/core/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 04286eb

Please sign in to comment.