diff options
author | 2022-06-03 15:38:57 +0200 | |
---|---|---|
committer | 2022-06-03 08:38:57 -0500 | |
commit | 6c955ca643a7a071609ce8a5258cc7faf5a636b2 (patch) | |
tree | 940ad0bf1c8f5fd61151ce1d842823433a6e7b1d /packages/markdown/remark/src | |
parent | 30578015919e019cd8dd354288a45c1fc63bd01f (diff) | |
download | astro-6c955ca643a7a071609ce8a5258cc7faf5a636b2.tar.gz astro-6c955ca643a7a071609ce8a5258cc7faf5a636b2.tar.zst astro-6c955ca643a7a071609ce8a5258cc7faf5a636b2.zip |
Fix Markdown errors missing source filename (#3514)
Diffstat (limited to 'packages/markdown/remark/src')
-rw-r--r-- | packages/markdown/remark/src/index.ts | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/packages/markdown/remark/src/index.ts b/packages/markdown/remark/src/index.ts index 5df5566a1..2e9aa54fd 100644 --- a/packages/markdown/remark/src/index.ts +++ b/packages/markdown/remark/src/index.ts @@ -109,6 +109,9 @@ export async function renderMarkdown( .process(input); result = vfile.toString(); } catch (err) { + // Ensure that the error message contains the input filename + // to make it easier for the user to fix the issue + err = prefixError(err, `Failed to parse Markdown file "${input.path}"`); console.error(err); throw err; } @@ -118,3 +121,27 @@ export async function renderMarkdown( code: result.toString(), }; } + +function prefixError(err: any, prefix: string) { + // If the error is an object with a `message` property, attempt to prefix the message + if (err && err.message) { + try { + err.message = `${prefix}:\n${err.message}`; + return err; + } catch (error) { + // Any errors here are ok, there's fallback code below + } + } + + // If that failed, create a new error with the desired message and attempt to keep the stack + const wrappedError = new Error(`${prefix}${err ? `: ${err}` : ''}`); + try { + wrappedError.stack = err.stack; + // @ts-ignore + wrappedError.cause = err; + } catch (error) { + // It's ok if we could not set the stack or cause - the message is the most important part + } + + return wrappedError; +} |