Skip to content

Commit

Permalink
Improve YAML error handling (#3557)
Browse files Browse the repository at this point in the history
* chore: improve YAML errors

* chore: add changeset
  • Loading branch information
natemoo-re committed Jun 10, 2022
1 parent 99ee40c commit 3ec41f2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-buckets-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Improve the way YAML errors are surfaced
14 changes: 14 additions & 0 deletions packages/astro/src/core/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ export function collectErrorMetadata(e: any): ErrorWithMetadata {
(e as any).stack = eol.lf((e as any).stack);
}


if (e.name === 'YAMLException') {
const err = e as SSRError;
err.loc = { file: (e as any).id, line: (e as any).mark.line, column: (e as any).mark.column }
err.message = (e as any).reason;

if (!err.frame) {
try {
const fileContents = fs.readFileSync(err.loc.file!, 'utf8');
err.frame = codeFrame(fileContents, err.loc);
} catch {}
}
}

// Astro error (thrown by esbuild so it needs to be formatted for Vite)
if (Array.isArray((e as any).errors)) {
const { location, pluginName, text } = (e as BuildResult).errors[0];
Expand Down
14 changes: 12 additions & 2 deletions packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { AstroConfig } from '../@types/astro';
import { pagesVirtualModuleId } from '../core/app/index.js';
import { prependForwardSlash } from '../core/path.js';
import { resolvePages, viteID } from '../core/util.js';
import { collectErrorMetadata } from '../core/errors.js';
import { PAGE_SSR_SCRIPT_ID } from '../vite-plugin-scripts/index.js';
import { getFileInfo } from '../vite-plugin-utils/index.js';

Expand All @@ -20,6 +21,15 @@ interface AstroPluginOptions {
const MARKDOWN_IMPORT_FLAG = '?mdImport';
const MARKDOWN_CONTENT_FLAG = '?content';

function safeMatter(source: string, id: string) {
try {
return matter(source);
} catch (e) {
(e as any).id = id;
throw collectErrorMetadata(e)
}
}

// TODO: Clean up some of the shared logic between this Markdown plugin and the Astro plugin.
// Both end up connecting a `load()` hook to the Astro compiler, and share some copy-paste
// logic in how that is done.
Expand Down Expand Up @@ -82,7 +92,7 @@ export default function markdown({ config }: AstroPluginOptions): Plugin {
const { fileId, fileUrl } = getFileInfo(id, config);

const source = await fs.promises.readFile(fileId, 'utf8');
const { data: frontmatter, content: rawContent } = matter(source);
const { data: frontmatter, content: rawContent } = safeMatter(source, fileId);
return {
code: `
// Static
Expand Down Expand Up @@ -127,7 +137,7 @@ export default function markdown({ config }: AstroPluginOptions): Plugin {
const hasInjectedScript = isPage && config._ctx.scripts.some((s) => s.stage === 'page-ssr');

// Extract special frontmatter keys
let { data: frontmatter, content: markdownContent } = matter(source);
let { data: frontmatter, content: markdownContent } = safeMatter(source, filename);

// Turn HTML comments into JS comments while preventing nested `*/` sequences
// from ending the JS comment by injecting a zero-width space
Expand Down

0 comments on commit 3ec41f2

Please sign in to comment.