Skip to content

Commit

Permalink
refactor: use AstroError for frontmatter error
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Dec 16, 2022
1 parent 62d05b3 commit 0013e4d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
29 changes: 16 additions & 13 deletions packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createServer, ErrorPayload as ViteErrorPayload, ViteDevServer } from 'v
import { AstroSettings } from '../@types/astro.js';
import { astroContentVirtualModPlugin } from './vite-plugin-content-virtual-mod.js';
import { fileURLToPath } from 'node:url';
import { AstroError, AstroErrorData } from '../core/errors/index.js';

export const collectionConfigParser = z.object({
schema: z.any().optional(),
Expand Down Expand Up @@ -64,20 +65,22 @@ export async function getEntryData(entry: Entry, collectionConfig: CollectionCon
if (parsed.success) {
data = parsed.data;
} else {
const formattedError = new Error(
[
`Could not parse frontmatter in ${String(entry.collection)}${String(entry.id)}`,
...parsed.error.errors.map((zodError) => zodError.message),
].join('\n')
);
(formattedError as any).loc = {
file: entry._internal.filePath,
line: getFrontmatterErrorLine(
entry._internal.rawData,
String(parsed.error.errors[0].path[0])
const formattedError = new AstroError({
...AstroErrorData.MarkdownContentSchemaValidationError,
message: AstroErrorData.MarkdownContentSchemaValidationError.message(
entry.collection,
entry.id,
parsed.error
),
column: 1,
};
location: {
file: entry._internal.filePath,
line: getFrontmatterErrorLine(
entry._internal.rawData,
String(parsed.error.errors[0].path[0])
),
column: 0,
},
});
throw formattedError;
}
}
Expand Down
26 changes: 26 additions & 0 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Additionally, this code, much like `@types/astro.ts`, is used to generate documentation, so make sure to pass
// your changes by our wonderful docs team before merging!

import type { ZodError } from 'zod';

interface ErrorData {
code: number;
title: string;
Expand Down Expand Up @@ -475,6 +477,30 @@ See https://docs.astro.build/en/guides/server-side-rendering/ for more informati
title: 'Failed to parse Markdown frontmatter.',
code: 6001,
},
/**
* @docs
* @message
* **Example error message:**<br/>
* Could not parse frontmatter in **blog** → **post.md**<br/>
* "title" is required.<br/>
* "date" must be a valid date.
* @description
* A Markdown document's frontmatter in `src/content/` does not match your collection schema.
* Make sure that all required fields are present, and that all fields are of the correct type.
* You can check against the collection schema in your `src/content/config` file.
* See the [Content collections documentation](https://docs.astro.build/en/guides/content-collections) for more information.
*/
MarkdownContentSchemaValidationError: {
title: 'Content collection frontmatter invalid.',
code: 6002,
message: (collection: string, entryId: string, error: ZodError) => {
return [
`${String(collection)}${String(entryId)} frontmatter does not match collection schema.`,
...error.errors.map((zodError) => zodError.message),
].join('\n');
},
hint: 'See https://docs.astro.build/en/guides/content-collections for more information on content schemas.',
},
// Config Errors - 7xxx
UnknownConfigError: {
title: 'Unknown configuration error.',
Expand Down

0 comments on commit 0013e4d

Please sign in to comment.