Skip to content

Commit

Permalink
chore: update to AstroErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Apr 28, 2023
1 parent 696b6e6 commit b2b98b6
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getDataEntryId,
reloadContentConfigObserver,
} from './utils.js';
import { AstroError } from '../core/errors/errors.js';

type ChokidarEvent = 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir';
type RawContentEvent = { name: ChokidarEvent; entry: string };
Expand Down Expand Up @@ -123,6 +124,7 @@ export async function createContentTypesGenerator({
const collection = normalizePath(
path.relative(fileURLToPath(contentPaths.contentDir), fileURLToPath(event.entry))
);
const collectionKey = JSON.stringify(collection);
// If directory is multiple levels deep, it is not a collection. Ignore event.
const isCollectionEvent = collection.split('/').length === 1;
if (!isCollectionEvent) return { shouldGenerateTypes: false };
Expand All @@ -135,7 +137,9 @@ export async function createContentTypesGenerator({
}
break;
case 'unlinkDir':
delete collectionEntryMap[JSON.stringify(collection)];
if (collectionKey in collectionEntryMap) {
delete collectionEntryMap[JSON.stringify(collection)];
}
break;
}
return { shouldGenerateTypes: true };
Expand Down Expand Up @@ -201,10 +205,10 @@ export async function createContentTypesGenerator({
}
const collectionInfo = collectionEntryMap[collectionKey];
if (collectionInfo.type === 'content') {
// TODO: AstroError
throw new Error(
`${collectionKey} contains a mix of content and data entries. All entries must be of the same type.`
);
throw new AstroError({
code: 99999,
message: `${collectionKey} contains a mix of content and data entries. All entries must be of the same type.`,
});
}
if (!(entryKey in collectionEntryMap[collectionKey])) {
collectionEntryMap[collectionKey] = {
Expand Down Expand Up @@ -236,10 +240,10 @@ export async function createContentTypesGenerator({
}
const collectionInfo = collectionEntryMap[collectionKey];
if (collectionInfo.type === 'data') {
// TODO: AstroError
throw new Error(
`${collectionKey} contains a mix of content and data entries. All entries must be of the same type.`
);
throw new AstroError({
code: 99999,
message: `${collectionKey} contains a mix of content and data entries. All entries must be of the same type.`,
});
}
const entryKey = JSON.stringify(id);

Expand Down Expand Up @@ -391,10 +395,14 @@ async function writeContentFiles({
const collectionConfig = contentConfig?.collections[JSON.parse(collectionKey)];
const collection = collectionEntryMap[collectionKey];
if (collectionConfig?.type && collection.type !== collectionConfig.type) {
// TODO: AstroError
throw new Error(
`${collectionKey} contains ${collection.type} entries, but is configured as ${collectionConfig.type}.`
);
throw new AstroError({
code: 99999,
message: `${collectionKey} contains ${collection.type} entries, but is configured as a ${collectionConfig.type} collection.`,
hint:
collection.type === 'data'
? "Try adding `type: 'data'` to your collection config."
: undefined,
});
}
switch (collection.type) {
case 'content':
Expand Down

0 comments on commit b2b98b6

Please sign in to comment.