Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Content collections] Surface content config errors in overlay #6170

Merged
merged 5 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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/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
20 changes: 15 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,8 @@ 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 { AstroErrorData } from '../core/errors/errors-data.js';
bholmesdev marked this conversation as resolved.
Show resolved Hide resolved
import { AstroError } 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 +119,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
3 changes: 3 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,9 @@ export function astroContentImportPlugin({
message: 'Content config failed to load.',
});
}
if (observable.status === 'error') {
throw observable.error;
}

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