Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/moody-apples-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Adds new content collections errors
10 changes: 4 additions & 6 deletions packages/astro/src/content/loaders/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import yaml from 'js-yaml';
import { AstroError } from '../../core/errors/index.js';
import { posixRelative } from '../utils.js';
import type { Loader, LoaderContext } from './types.js';
import { FileGlobNotSupported, FileParserNotFound } from '../../core/errors/errors-data.js';

interface FileOptions {
/**
Expand All @@ -22,10 +23,7 @@ interface FileOptions {
*/
export function file(fileName: string, options?: FileOptions): Loader {
if (fileName.includes('*')) {
throw new AstroError({
name: 'Glob Pattern Error',
message: 'Glob patterns are not supported in `file` loader. Use `glob` loader instead.',
});
throw new AstroError(FileGlobNotSupported);
}

let parse: ((text: string) => any) | null = null;
Expand All @@ -43,8 +41,8 @@ export function file(fileName: string, options?: FileOptions): Loader {

if (parse === null) {
throw new AstroError({
name: 'Parser Not Found',
message: `No parser found for file '${fileName}'. Try passing a parser to the \`file\` loader.`,
...FileParserNotFound,
message: FileParserNotFound.message(fileName),
});
}

Expand Down
31 changes: 31 additions & 0 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,7 @@ export const GenerateContentTypesError = {
hint: (fileName?: string) =>
`This error is often caused by a syntax error inside your content, or your content configuration file. Check your ${fileName ?? 'content config'} file for typos.`,
} satisfies ErrorData;

/**
* @docs
* @kind heading
Expand Down Expand Up @@ -1815,6 +1816,36 @@ export const UnsupportedConfigTransformError = {
hint: 'See the devalue library for all supported types: https://github.com/rich-harris/devalue',
} satisfies ErrorData;

/**
* @docs
* @see
* - [Passing a `parser` to the `file` loader](https://docs.astro.build/en/guides/imports/#parsers)
* @description
* The `file` loader can’t determine which parser to use. Please provide a custom parser (e.g. `toml.parse` or `csv-parse`) to create a collection from your file type.
*/
export const FileParserNotFound = {
name: 'FileParserNotFound',
title: 'File parser not found',
message: (fileName: string) =>
`No parser was found for '${fileName}'. Pass a parser function (e.g. \`parser: csv\`) to the \`file\` loader.`,
} satisfies ErrorData;

/**
* @docs
* @see
* - [Astro's built-in loaders](https://docs.astro.build/en/guides/content-collections/#built-in-loaders)
* @description
* The `file` loader must be passed a single local file. Glob patterns are not supported. Use the built-in `glob` loader to create entries from patterns of multiple local files.
*/
export const FileGlobNotSupported = {
name: 'FileGlobNotSupported',
title: 'Glob patterns are not supported in the file loader',
message:
'Glob patterns are not supported in the `file` loader. Use the `glob` loader instead.',
hint:
`See Astro's built-in file and glob loaders https://docs.astro.build/en/guides/content-collections/#built-in-loaders for supported usage.`,
} satisfies ErrorData;

/**
* @docs
* @kind heading
Expand Down
Loading