From 8aabf74e449ae165cabfe5846fe641312b542750 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Tue, 26 Sep 2023 18:56:37 +0200 Subject: [PATCH] Change to improve error messages --- lib/ast-to-react.js | 6 +++++- lib/react-markdown.js | 10 ++++++---- lib/rehype-filter.js | 2 +- test/test.jsx | 8 ++++---- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/ast-to-react.js b/lib/ast-to-react.js index 07b6a3ce..4e3731b0 100644 --- a/lib/ast-to-react.js +++ b/lib/ast-to-react.js @@ -193,7 +193,11 @@ function toReact(state, node, index, parent) { if (!basic && typeof component !== 'function') { throw new Error( - `Component for name \`${name}\` not defined or is not renderable` + 'Unexpected value `' + + component + + '` for `' + + name + + '`, expected component or tag name' ) } diff --git a/lib/react-markdown.js b/lib/react-markdown.js index 2aeb1372..4d08e366 100644 --- a/lib/react-markdown.js +++ b/lib/react-markdown.js @@ -180,17 +180,19 @@ export function ReactMarkdown(options) { ) } - const hastNode = processor.runSync(processor.parse(file), file) + const hastTree = processor.runSync(processor.parse(file), file) - if (hastNode.type !== 'root') { - throw new TypeError('Expected a `root` node') + if (hastTree.type !== 'root') { + throw new TypeError( + 'Unexpected `' + hastTree.type + '` node, expected `root`' + ) } /** @type {ReactElement} */ let result = React.createElement( React.Fragment, {}, - childrenToReact({options, schema: html, listDepth: 0}, hastNode) + childrenToReact({options, schema: html, listDepth: 0}, hastTree) ) if (options.className) { diff --git a/lib/rehype-filter.js b/lib/rehype-filter.js index 9da34f48..03121bad 100644 --- a/lib/rehype-filter.js +++ b/lib/rehype-filter.js @@ -22,7 +22,7 @@ export default function rehypeFilter(options) { ) { if (options.allowedElements && options.disallowedElements) { throw new TypeError( - 'Only one of `allowedElements` and `disallowedElements` should be defined' + 'Unexpected combined `allowedElements` and `disallowedElements`, expected one or the other' ) } diff --git a/test/test.jsx b/test/test.jsx index e2c31d30..23b3ecad 100644 --- a/test/test.jsx +++ b/test/test.jsx @@ -558,7 +558,7 @@ test('react-markdown', async function (t) { disallowedElements={['a']} /> ) - }, /only one of/i) + }, /Unexpected combined `allowedElements` and `disallowedElements`, expected one or the other/) } ) @@ -631,7 +631,7 @@ test('react-markdown', async function (t) { ) }) - await t.test('should fail on invalid component', function () { + await t.test('should fail on an invalid component', function () { assert.throws(function () { asHtml( ) - }, /Component for name `h1`/) + }, /Unexpected value `123` for `h1`, expected component or tag name/) }) await t.test('should support `null`, `undefined` in components', function () { @@ -1204,7 +1204,7 @@ test('react-markdown', async function (t) { await t.test('should fail on a plugin replacing `root`', function () { assert.throws(function () { asHtml() - }, /Expected a `root` node/) + }, /Unexpected `comment` node, expected `root/) function plugin() { /**