0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

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

* 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
This commit is contained in:
Ben Holmes 2023-02-08 10:43:38 -05:00 committed by GitHub
parent 86d5479c6f
commit ec2f2a31de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 7 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Show content config errors in overlay, instead of stopping the dev server.

View file

@ -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':

View file

@ -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';
@ -117,11 +118,19 @@ export async function createContentTypesGenerator({
}
if (fileType === 'config') {
contentConfigObserver.set({ status: 'loading' });
try {
const config = await loadContentConfig({ fs, settings, viteServer });
if (config) {
contentConfigObserver.set({ status: 'loaded', config });
} else {
contentConfigObserver.set({ status: 'error' });
contentConfigObserver.set({ status: 'does-not-exist' });
}
} catch (e) {
contentConfigObserver.set({
status: 'error',
error:
e instanceof Error ? e : new AstroError(AstroErrorData.UnknownContentCollectionError),
});
}
return { shouldGenerateTypes: true };

View file

@ -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;

View file

@ -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;