Skip to content

Commit

Permalink
fix: throw a useful error if rendering undefined entry
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Oct 3, 2024
1 parent 34d7952 commit b304af3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/wise-pumas-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Throw a helpful error if attempting to render undefined entry
6 changes: 5 additions & 1 deletion packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,11 @@ function updateImageReferencesInData<T extends Record<string, unknown>>(
export async function renderEntry(
entry: DataEntry | { render: () => Promise<{ Content: AstroComponentFactory }> },
) {
if (entry && 'render' in entry) {
if (!entry) {
throw new AstroError(AstroErrorData.RenderUndefinedEntryError);
}

if ('render' in entry) {
// This is an old content collection entry, so we use its render method
return entry.render();
}
Expand Down
11 changes: 11 additions & 0 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,17 @@ export const UnknownContentCollectionError = {
title: 'Unknown Content Collection Error.',
} satisfies ErrorData;

/**
* @docs
* @description
* Astro tried to render a content collection entry that was undefined. This can happen if you try to render an entry that does not exist.
*/
export const RenderUndefinedEntryError = {
name: 'RenderUndefinedEntryError',
title: 'Attempted to render an undefined content collection entry.',
hint: 'Check if the entry is undefined before passing it to `render()`',
} satisfies ErrorData;

/**
* @docs
* @description
Expand Down
7 changes: 7 additions & 0 deletions packages/astro/test/content-layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,5 +317,12 @@ describe('Content Layer', () => {
assert.ok(updated.fileLoader[0].data.temperament.includes('Bouncy'));
await fixture.resetAllFiles();
});

it('returns an error if we render an undefined entry', async () => {
const res = await fixture.fetch('/missing');
const text = await res.text();
assert.equal(res.status, 500);
assert.ok(text.includes('RenderUndefinedEntryError'));
});
});
});
14 changes: 14 additions & 0 deletions packages/astro/test/fixtures/content-layer/src/pages/missing.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
import { getEntry, render } from "astro:content"
// Skipping the broken page in production so the build doesn't fail
if(import.meta.env.PROD) {
return new Response(null, { status: 404 })
}
const entry = await getEntry("spacecraft", "missing")
const { Content } = await render(entry)
---

<Content />

0 comments on commit b304af3

Please sign in to comment.