Skip to content

Commit 39d330f

Browse files
Princesseuhdelucis
andauthored
docs: rework docs around errors (#11802)
* docs: rework docs around errors * Update packages/astro/src/core/errors/README.md Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * docs: expand deprecation description --------- Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
1 parent 645f4e2 commit 39d330f

File tree

2 files changed

+23
-92
lines changed

2 files changed

+23
-92
lines changed

packages/astro/src/core/errors/README.md

+22-6
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ Message:
2121

2222
- Begin with **what happened** and **why**. (ex: `Could not use {feature} because Server-side Rendering is not enabled.`)
2323
- Then, **describe the action the user should take**. (ex: `Update your Astro config with `output: 'server'` to enable Server-side Rendering.`)
24-
- Although this does not need to be as brief as the `title`, try to keep sentences short, clear and direct to give the reader all the necessary information quickly as possible.
25-
- Instead of writing a longer message, consider using a `hint`.
24+
- Although this does not need to be as brief as the `title`, try to keep sentences short, clear and direct to give the reader all the necessary information quickly as possible. Users should be able to skim the message and understand the problem and solution.
25+
- If your message is too long, or the solution is not guaranteed to work, use the `hint` property to provide more information.
2626

2727
Hint:
2828

@@ -44,10 +44,10 @@ If you are unsure about anything, ask [Erika](https://github.com/Princesseuh)!
4444

4545
### Shape
4646

47-
- **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.
47+
- **Names are permanent**, and should never be changed. Users should always be able to find an error by searching, and this ensures a matching result.
4848
- 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.
4949
- 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.
50-
- Dynamic error messages must use the following shape:
50+
- Dynamic error messages **must** use the following shape:
5151

5252
```js
5353
message: (arguments) => `text ${substitute}`;
@@ -69,8 +69,9 @@ Here's how to create and format the comments:
6969
/**
7070
* @docs <- Needed for the comment to be used for docs
7171
* @message <- (Optional) Clearer error message to show in cases where the original one is too complex (ex: because of conditional messages)
72-
* @see <- List of additional references users can look at
72+
* @see <- (Optional) List of additional references users can look at
7373
* @description <- Description of the error
74+
* @deprecated <- (Optional) If the error is no longer relevant, when it was removed and why (see "Removing errors" section below)
7475
*/
7576
```
7677

@@ -89,6 +90,19 @@ Example:
8990

9091
The `@message` property is intended to provide slightly more context when it is helpful: a more descriptive error message or a collection of common messages if there are multiple possible error messages. Try to avoid making substantial changes to existing messages so that they are easy to find for users who copy and search the exact content of an error message.
9192

93+
### Removing errors
94+
95+
If the error cannot be triggered at all anymore, it can deprecated by adding a `@deprecated` tag to the JSDoc comment with a message that will be shown in the docs. This message is useful for users on previous versions who might still encounter the error so that they can know that upgrading to a newer version of Astro would perhaps solve their issue.
96+
97+
```js
98+
/**
99+
* @docs
100+
* @deprecated Removed in Astro v9.8.6 as it is no longer relevant due to...
101+
*/
102+
```
103+
104+
Alternatively, if no special deprecation message is needed, errors can be directly removed from the `errors-data.ts` file. A basic message will be shown in the docs stating that the error can no longer appear in the latest version of Astro.
105+
92106
### Always remember
93107

94108
Error are a reactive strategy. They are the last line of defense against a mistake.
@@ -99,5 +113,7 @@ While adding a new error message, ask yourself, "Was there a way this situation
99113

100114
## Additional resources on writing good error messages
101115

116+
- [Compiler errors for humans](https://elm-lang.org/news/compiler-errors-for-humans)
102117
- [When life gives you lemons, write better error messages](https://wix-ux.com/when-life-gives-you-lemons-write-better-error-messages-46c5223e1a2f)
103-
- [RustConf 2020 - Bending the Curve: A Personal Tutor at Your Fingertips by Esteban Kuber](https://www.youtube.com/watch?v=Z6X7Ada0ugE) (part on error messages starts around 19:17)
118+
- [RustConf 2020 - Bending the Curve: A Personal Tutor at Your Fingertips by Esteban Kuber](https://www.youtube.com/watch?v=Z6X7Ada0ugE)
119+
- [What's in a good error](https://erika.florist/articles/gooderrors) (by the person who wrote this document!)

packages/astro/src/core/errors/errors-data.ts

+1-86
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,7 @@ export const UnknownCompilerError = {
3333
title: 'Unknown compiler error.',
3434
hint: 'This is almost always a problem with the Astro compiler, not your code. Please open an issue at https://astro.build/issues/compiler.',
3535
} satisfies ErrorData;
36-
// 1xxx and 2xxx codes are reserved for compiler errors and warnings respectively
37-
/**
38-
* @docs
39-
* @see
40-
* - [Enabling SSR in Your Project](https://docs.astro.build/en/guides/server-side-rendering/)
41-
* - [Astro.redirect](https://docs.astro.build/en/reference/api-reference/#astroredirect)
42-
* @description
43-
* The `Astro.redirect` function is only available when [Server-side rendering](/en/guides/server-side-rendering/) is enabled.
44-
*
45-
* To redirect on a static website, the [meta refresh attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta) can be used. Certain hosts also provide config-based redirects (ex: [Netlify redirects](https://docs.netlify.com/routing/redirects/)).
46-
* @deprecated Deprecated since version 2.6.
47-
*/
48-
export const StaticRedirectNotAvailable = {
49-
name: 'StaticRedirectNotAvailable',
50-
title: '`Astro.redirect` is not available in static mode.',
51-
message:
52-
"Redirects are only available when using `output: 'server'` or `output: 'hybrid'`. Update your Astro config if you need SSR features.",
53-
hint: 'See https://docs.astro.build/en/guides/server-side-rendering/ for more information on how to enable SSR.',
54-
} satisfies ErrorData;
36+
5537
/**
5638
* @docs
5739
* @see
@@ -302,21 +284,6 @@ export const InvalidGetStaticPathsReturn = {
302284
hint: 'See https://docs.astro.build/en/reference/api-reference/#getstaticpaths for more information on getStaticPaths.',
303285
} satisfies ErrorData;
304286

305-
/**
306-
* @docs
307-
* @deprecated Deprecated since Astro 4.0. The RSS helper no longer exists with an error fallback.
308-
* @see
309-
* - [RSS Guide](https://docs.astro.build/en/guides/rss/)
310-
* @description
311-
* `getStaticPaths` no longer expose an helper for generating a RSS feed. We recommend migrating to the [@astrojs/rss](https://docs.astro.build/en/guides/rss/#setting-up-astrojsrss)integration instead.
312-
*/
313-
export const GetStaticPathsRemovedRSSHelper = {
314-
name: 'GetStaticPathsRemovedRSSHelper',
315-
title: 'getStaticPaths RSS helper is not available anymore.',
316-
message:
317-
'The RSS helper has been removed from `getStaticPaths`. Try the new @astrojs/rss package instead.',
318-
hint: 'See https://docs.astro.build/en/guides/rss/ for more information.',
319-
} satisfies ErrorData;
320287
/**
321288
* @docs
322289
* @see
@@ -732,28 +699,6 @@ export const NoImageMetadata = {
732699
hint: 'This is often caused by a corrupted or malformed image. Re-exporting the image from your image editor may fix this issue.',
733700
} satisfies ErrorData;
734701

735-
/**
736-
* @docs
737-
* @deprecated This error is no longer Markdown specific and as such, as been replaced by `ImageNotFound`
738-
* @message
739-
* Could not find requested image `IMAGE_PATH` at `FULL_IMAGE_PATH`.
740-
* @see
741-
* - [Images](https://docs.astro.build/en/guides/images/)
742-
* @description
743-
* Astro could not find an image you included in your Markdown content. Usually, this is simply caused by a typo in the path.
744-
*
745-
* Images in Markdown are relative to the current file. To refer to an image that is located in the same folder as the `.md` file, the path should start with `./`
746-
*/
747-
export const MarkdownImageNotFound = {
748-
name: 'MarkdownImageNotFound',
749-
title: 'Image not found.',
750-
message: (imagePath: string, fullImagePath: string | undefined) =>
751-
`Could not find requested image \`${imagePath}\`${
752-
fullImagePath ? ` at \`${fullImagePath}\`.` : '.'
753-
}`,
754-
hint: 'This is often caused by a typo in the image path. Please make sure the file exists, and is spelled correctly.',
755-
} satisfies ErrorData;
756-
757702
/**
758703
* @docs
759704
* @see
@@ -1140,22 +1085,6 @@ export const MissingMiddlewareForInternationalization = {
11401085
"Your configuration setting `i18n.routing: 'manual'` requires you to provide your own i18n `middleware` file.",
11411086
} satisfies ErrorData;
11421087

1143-
/**
1144-
* @deprecated
1145-
* @docs
1146-
* @description
1147-
* The user tried to rewrite using a route that doesn't exist, or it emitted a runtime error during its rendering phase.
1148-
*/
1149-
export const RewriteEncounteredAnError = {
1150-
name: 'RewriteEncounteredAnError',
1151-
title:
1152-
"Astro couldn't find the route to rewrite, or if was found but it emitted an error during the rendering phase.",
1153-
message: (route: string, stack?: string) =>
1154-
`The route ${route} that you tried to render doesn't exist, or it emitted an error during the rendering phase. ${
1155-
stack ? stack : ''
1156-
}.`,
1157-
} satisfies ErrorData;
1158-
11591088
/**
11601089
* @docs
11611090
* @description
@@ -1553,20 +1482,6 @@ export const ContentSchemaContainsSlugError = {
15531482
hint: 'See https://docs.astro.build/en/guides/content-collections/ for more on the `slug` field.',
15541483
} satisfies ErrorData;
15551484

1556-
/**
1557-
* @docs
1558-
* @message A collection queried via `getCollection()` does not exist.
1559-
* @deprecated Collections that do not exist no longer result in an error. A warning is given instead.
1560-
* @description
1561-
* When querying a collection, ensure a collection directory with the requested name exists under `src/content/`.
1562-
*/
1563-
export const CollectionDoesNotExistError = {
1564-
name: 'CollectionDoesNotExistError',
1565-
title: 'Collection does not exist',
1566-
message: (collectionName: string) =>
1567-
`The collection **${collectionName}** does not exist. Ensure a collection directory with this name exists.`,
1568-
hint: 'See https://docs.astro.build/en/guides/content-collections/ for more on creating collections.',
1569-
} satisfies ErrorData;
15701485
/**
15711486
* @docs
15721487
* @see

0 commit comments

Comments
 (0)