Skip to content

Commit

Permalink
[Content collections] Surface content config errors in overlay (#6170)
Browse files Browse the repository at this point in the history
* fix: throw error during import to catch error boundary

* fix: throw from astro sync

* chore: changeset

* chore: add comment on why error here

* chore: consolidate imports
  • Loading branch information
bholmesdev authored Feb 8, 2023
1 parent 86d5479 commit ec2f2a3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-kiwis-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Show content config errors in overlay, instead of stopping the dev server.
6 changes: 6 additions & 0 deletions packages/astro/src/cli/sync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export async function sync(
viteServer: tempViteServer,
});
const typesResult = await contentTypesGenerator.init();

const contentConfig = globalContentConfigObserver.get();
if (contentConfig.status === 'error') {
throw contentConfig.error;
}

if (typesResult.typesGenerated === false) {
switch (typesResult.reason) {
case 'no-content-dir':
Expand Down
19 changes: 14 additions & 5 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { normalizePath, ViteDevServer } from 'vite';
import type { AstroSettings } from '../@types/astro.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { info, LogOptions, warn } from '../core/logger/core.js';
import { isRelativePath } from '../core/path.js';
import { CONTENT_TYPES_FILE } from './consts.js';
Expand Down Expand Up @@ -117,11 +118,19 @@ export async function createContentTypesGenerator({
}
if (fileType === 'config') {
contentConfigObserver.set({ status: 'loading' });
const config = await loadContentConfig({ fs, settings, viteServer });
if (config) {
contentConfigObserver.set({ status: 'loaded', config });
} else {
contentConfigObserver.set({ status: 'error' });
try {
const config = await loadContentConfig({ fs, settings, viteServer });
if (config) {
contentConfigObserver.set({ status: 'loaded', config });
} else {
contentConfigObserver.set({ status: 'does-not-exist' });
}
} catch (e) {
contentConfigObserver.set({
status: 'error',
error:
e instanceof Error ? e : new AstroError(AstroErrorData.UnknownContentCollectionError),
});
}

return { shouldGenerateTypes: true };
Expand Down
5 changes: 3 additions & 2 deletions packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,9 @@ export async function loadContentConfig({
type ContentCtx =
| { status: 'init' }
| { status: 'loading' }
| { status: 'error' }
| { status: 'loaded'; config: ContentConfig };
| { status: 'does-not-exist' }
| { status: 'loaded'; config: ContentConfig }
| { status: 'error'; error: Error };

type Observable<C> = {
get: () => C;
Expand Down
5 changes: 5 additions & 0 deletions packages/astro/src/content/vite-plugin-content-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export function astroContentImportPlugin({
message: 'Content config failed to load.',
});
}
if (observable.status === 'error') {
// Throw here to bubble content config errors
// to the error overlay in development
throw observable.error;
}

let contentConfig: ContentConfig | undefined =
observable.status === 'loaded' ? observable.config : undefined;
Expand Down

0 comments on commit ec2f2a3

Please sign in to comment.