Skip to content

Commit

Permalink
Fix: better collection entry devalue error (#7341)
Browse files Browse the repository at this point in the history
* feat: add clear error for devalue parse

* refactor: create error data entry

* deps: up devalue

* chore: changeset
  • Loading branch information
bholmesdev committed Jun 9, 2023
1 parent f4f9524 commit 491c2db
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changeset/seven-carpets-accept.md
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/markdoc': patch
---

Improve error message for unsupported Zod transforms from the content config.
2 changes: 1 addition & 1 deletion packages/astro/package.json
Expand Up @@ -134,7 +134,7 @@
"cookie": "^0.5.0",
"debug": "^4.3.4",
"deepmerge-ts": "^4.2.2",
"devalue": "^4.2.0",
"devalue": "^4.3.2",
"diff": "^5.1.0",
"es-module-lexer": "^1.1.0",
"esbuild": "^0.17.18",
Expand Down
29 changes: 27 additions & 2 deletions packages/astro/src/content/vite-plugin-content-imports.ts
Expand Up @@ -94,7 +94,7 @@ export function astroContentImportPlugin({
const code = escapeViteEnvReferences(`
export const id = ${JSON.stringify(id)};
export const collection = ${JSON.stringify(collection)};
export const data = ${devalue.uneval(data) /* TODO: reuse astro props serializer */};
export const data = ${stringifyEntryData(data)};
export const _internal = {
type: 'data',
filePath: ${JSON.stringify(_internal.filePath)},
Expand All @@ -118,7 +118,7 @@ export const _internal = {
export const collection = ${JSON.stringify(collection)};
export const slug = ${JSON.stringify(slug)};
export const body = ${JSON.stringify(body)};
export const data = ${devalue.uneval(data) /* TODO: reuse astro props serializer */};
export const data = ${stringifyEntryData(data)};
export const _internal = {
type: 'content',
filePath: ${JSON.stringify(_internal.filePath)},
Expand Down Expand Up @@ -352,3 +352,28 @@ async function getContentConfigFromGlobal() {

return contentConfig;
}

/** Stringify entry `data` at build time to be used as a Vite module */
function stringifyEntryData(data: Record<string, any>): string {
try {
return devalue.uneval(data, (value) => {
// Add support for URL objects
if (value instanceof URL) {
return `new URL(${JSON.stringify(value.href)})`;
}
});
} catch (e) {
if (e instanceof Error) {
throw new AstroError({
...AstroErrorData.UnsupportedConfigTransformError,
message: AstroErrorData.UnsupportedConfigTransformError.message(e.message),
stack: e.stack,
});
} else {
throw new AstroError({
code: 99999,
message: 'Unexpected error processing content collection data.',
});
}
}
}
15 changes: 15 additions & 0 deletions packages/astro/src/core/errors/errors-data.ts
Expand Up @@ -1113,6 +1113,21 @@ See https://docs.astro.build/en/guides/server-side-rendering/ for more informati
hint: 'Ensure your data entry is an object with valid JSON (for `.json` entries) or YAML (for `.yaml` entries).',
},

/**
* @docs
* @see
* - [devalue library](https://github.com/rich-harris/devalue)
* @description
* `transform()` functions in your content config must return valid JSON, or data types compatible with the devalue library (including Dates, Maps, and Sets).
*/
UnsupportedConfigTransformError: {
title: 'Unsupported transform in content config.',
code: 9008,
message: (parseError: string) =>
`\`transform()\` functions in your content config must return valid JSON, or data types compatible with the devalue library (including Dates, Maps, and Sets).\nFull error: ${parseError}`,
hint: 'See the devalue library for all supported types: https://github.com/rich-harris/devalue',
},

// Generic catch-all - Only use this in extreme cases, like if there was a cosmic ray bit flip
UnknownError: {
title: 'Unknown Error.',
Expand Down
4 changes: 2 additions & 2 deletions packages/integrations/markdoc/package.json
Expand Up @@ -63,13 +63,13 @@
"test:match": "mocha --timeout 20000 -g"
},
"dependencies": {
"shiki": "^0.14.1",
"@astrojs/prism": "^2.1.2",
"@markdoc/markdoc": "^0.3.0",
"esbuild": "^0.17.12",
"github-slugger": "^2.0.0",
"gray-matter": "^4.0.3",
"kleur": "^4.1.5",
"shiki": "^0.14.1",
"zod": "^3.17.3"
},
"peerDependencies": {
Expand All @@ -83,7 +83,7 @@
"astro": "workspace:*",
"astro-scripts": "workspace:*",
"chai": "^4.3.6",
"devalue": "^4.2.0",
"devalue": "^4.3.2",
"linkedom": "^0.14.12",
"mocha": "^9.2.2",
"rollup": "^3.20.1",
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 491c2db

Please sign in to comment.