Skip to content

Commit

Permalink
fix(errors): Deprecate error codes (#7347)
Browse files Browse the repository at this point in the history
Co-authored-by: bluwy <bjornlu.dev@gmail.com>
  • Loading branch information
Princesseuh and bluwy committed Jun 26, 2023
1 parent 39a24f1 commit 4e02a59
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 195 deletions.
1 change: 0 additions & 1 deletion packages/astro/src/content/vite-plugin-content-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ function stringifyEntryData(data: Record<string, any>): string {
});
} else {
throw new AstroError({
code: 99999,
message: 'Unexpected error processing content collection data.',
});
}
Expand Down
5 changes: 0 additions & 5 deletions packages/astro/src/core/compile/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ function handleCompileResultErrors(result: TransformResult, cssTransformErrors:

if (compilerError) {
throw new CompilerError({
code: compilerError.code,
message: compilerError.text,
location: {
line: compilerError.location.line,
Expand All @@ -98,15 +97,11 @@ function handleCompileResultErrors(result: TransformResult, cssTransformErrors:
break;
case 1: {
const error = cssTransformErrors[0];
if (!error.errorCode) {
error.errorCode = AstroErrorData.UnknownCSSError.code;
}
throw cssTransformErrors[0];
}
default: {
throw new AggregateError({
...cssTransformErrors[0],
code: cssTransformErrors[0].errorCode,
errors: cssTransformErrors,
});
}
Expand Down
1 change: 0 additions & 1 deletion packages/astro/src/core/compile/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ function enhanceCSSError(err: any, filename: string) {
errorPosition.line += 1;

return new CSSError({
code: AstroErrorData.UnknownCSSError.code,
message: err.message,
location: {
file: filename,
Expand Down
25 changes: 4 additions & 21 deletions packages/astro/src/core/errors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

Name (key of the object definition):

- As with the error code, this property is a static reference to the error. The shape should be similar to JavaScript's native errors (TypeError, ReferenceError): pascal-cased, no spaces, no special characters etc. (ex: `ClientAddressNotAvailable`)
- This property is a static reference to the error. The shape should be similar to JavaScript's native errors (TypeError, ReferenceError): pascal-cased, no spaces, no special characters etc. (ex: `ClientAddressNotAvailable`)
- This is the only part of the error message that should not be written as a full, proper sentence complete with Capitalization and end punctuation.

Title:
Expand All @@ -35,24 +35,7 @@ Hint:
- Describe the _what_, _why_ and _action to take_ from the user's perspective. Assume they don't know Astro internals, and care only about how Astro is _used_. (ex: `You are missing...` vs `Astro/file cannot find...`)
- Avoid using cutesy language. (ex: Oops!) This tone minimizes the significance of the error, which _is_ important to the developer. The developer may be frustrated and your error message shouldn't be making jokes about their struggles. Only include words and phrases that help the developer **interpret the error** and **fix the problem**.

**Choosing an Error Code**

Choose any available error code in the appropriate range:

- 01xxx and 02xxx are reserved for compiler errors and warnings respectively
- 03xxx: Astro errors (your error most likely goes here!)
- 04xxx: Vite errors
- 05xxx: CSS errors
- 06xxx: Markdown errors
- 07xxx: Configuration errors
- 07xxx-98xxx <- Need to add a category? Add it here!
- 99xxx: Catch-alls for unknown errors

As long as it is unique, the exact error code used is unimportant. For example, error 5005 and error 5006 don't necessarily have to be related, or follow any logical pattern.

Users are not reading codes sequentially. They're much more likely to directly land on the error or search for a specific code.

If you are unsure about which error code to choose, ask [Erika](https://github.com/Princesseuh)!
If you are unsure about anything, ask [Erika](https://github.com/Princesseuh)!

### CLI specifics tips:

Expand All @@ -61,7 +44,7 @@ If you are unsure about which error code to choose, ask [Erika](https://github.c

### Shape

- **Error codes and names are permanent**, and should never be changed, nor deleted. Users should always be able to find an error by searching, and this ensures a matching result. When an error is no longer relevant, it should be deprecated, not removed.
- **Names are permanent**, and should never be changed, nor deleted. Users should always be able to find an error by searching, and this ensures a matching result. When an error is no longer relevant, it should be deprecated, not removed.
- Contextual information may be used to enhance the message or the hint. However, the code that caused the error or the position of the error should not be included in the message as they will already be shown as part of the error.
- Do not prefix `title`, `message` and `hint` with descriptive words such as "Error:" or "Hint:" as it may lead to duplicated labels in the UI / CLI.
- Dynamic error messages must use the following shape:
Expand All @@ -78,7 +61,7 @@ Using light logic to add / remove different parts of the message is okay, howeve

### Documentation support through JSDoc

Using JSDoc comments, [a reference for every error message](https://docs.astro.build/en/reference/error-reference/) is built automatically on our docs. Users can then search for a error code to find more information on how to fix the error they encountered.
Using JSDoc comments, [a reference for every error message](https://docs.astro.build/en/reference/error-reference/) is built automatically on our docs.

Here's how to create and format the comments:

Expand Down
14 changes: 1 addition & 13 deletions packages/astro/src/core/errors/dev/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export function enhanceViteSSRError({
safeError.name = 'FailedToLoadModuleSSR';
safeError.message = AstroErrorData.FailedToLoadModuleSSR.message(importName);
safeError.hint = AstroErrorData.FailedToLoadModuleSSR.hint;
safeError.code = AstroErrorData.FailedToLoadModuleSSR.code;
const line = lns.findIndex((ln) => ln.includes(importName!));

if (line !== -1) {
Expand Down Expand Up @@ -84,7 +83,6 @@ export function enhanceViteSSRError({
safeError.message = AstroErrorData.InvalidGlob.message(globPattern);
safeError.name = 'InvalidGlob';
safeError.hint = AstroErrorData.InvalidGlob.hint;
safeError.code = AstroErrorData.InvalidGlob.code;
safeError.title = AstroErrorData.InvalidGlob.title;

const line = lns.findIndex((ln) => ln.includes(globPattern));
Expand Down Expand Up @@ -139,17 +137,7 @@ export async function getViteErrorPayload(err: ErrorWithMetadata): Promise<Astro
const message = renderErrorMarkdown(err.message.trim(), 'html');
const hint = err.hint ? renderErrorMarkdown(err.hint.trim(), 'html') : undefined;

const hasDocs =
(err.type &&
err.name && [
'AstroError',
'AggregateError',
/* 'CompilerError' ,*/
'CSSError',
'MarkdownError',
]) ||
['FailedToLoadModuleSSR', 'InvalidGlob'].includes(err.name);

const hasDocs = err.name in AstroErrorData;
const docslink = hasDocs
? `https://docs.astro.build/en/reference/errors/${getKebabErrorName(err.name)}/`
: undefined;
Expand Down
Loading

0 comments on commit 4e02a59

Please sign in to comment.