Skip to content

Commit 8aabf74

Browse files
committed
Change to improve error messages
1 parent 8b1ff41 commit 8aabf74

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

lib/ast-to-react.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,11 @@ function toReact(state, node, index, parent) {
193193

194194
if (!basic && typeof component !== 'function') {
195195
throw new Error(
196-
`Component for name \`${name}\` not defined or is not renderable`
196+
'Unexpected value `' +
197+
component +
198+
'` for `' +
199+
name +
200+
'`, expected component or tag name'
197201
)
198202
}
199203

lib/react-markdown.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,19 @@ export function ReactMarkdown(options) {
180180
)
181181
}
182182

183-
const hastNode = processor.runSync(processor.parse(file), file)
183+
const hastTree = processor.runSync(processor.parse(file), file)
184184

185-
if (hastNode.type !== 'root') {
186-
throw new TypeError('Expected a `root` node')
185+
if (hastTree.type !== 'root') {
186+
throw new TypeError(
187+
'Unexpected `' + hastTree.type + '` node, expected `root`'
188+
)
187189
}
188190

189191
/** @type {ReactElement} */
190192
let result = React.createElement(
191193
React.Fragment,
192194
{},
193-
childrenToReact({options, schema: html, listDepth: 0}, hastNode)
195+
childrenToReact({options, schema: html, listDepth: 0}, hastTree)
194196
)
195197

196198
if (options.className) {

lib/rehype-filter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function rehypeFilter(options) {
2222
) {
2323
if (options.allowedElements && options.disallowedElements) {
2424
throw new TypeError(
25-
'Only one of `allowedElements` and `disallowedElements` should be defined'
25+
'Unexpected combined `allowedElements` and `disallowedElements`, expected one or the other'
2626
)
2727
}
2828

test/test.jsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ test('react-markdown', async function (t) {
558558
disallowedElements={['a']}
559559
/>
560560
)
561-
}, /only one of/i)
561+
}, /Unexpected combined `allowedElements` and `disallowedElements`, expected one or the other/)
562562
}
563563
)
564564

@@ -631,7 +631,7 @@ test('react-markdown', async function (t) {
631631
)
632632
})
633633

634-
await t.test('should fail on invalid component', function () {
634+
await t.test('should fail on an invalid component', function () {
635635
assert.throws(function () {
636636
asHtml(
637637
<Markdown
@@ -642,7 +642,7 @@ test('react-markdown', async function (t) {
642642
}}
643643
/>
644644
)
645-
}, /Component for name `h1`/)
645+
}, /Unexpected value `123` for `h1`, expected component or tag name/)
646646
})
647647

648648
await t.test('should support `null`, `undefined` in components', function () {
@@ -1204,7 +1204,7 @@ test('react-markdown', async function (t) {
12041204
await t.test('should fail on a plugin replacing `root`', function () {
12051205
assert.throws(function () {
12061206
asHtml(<Markdown children="a" rehypePlugins={[plugin]} />)
1207-
}, /Expected a `root` node/)
1207+
}, /Unexpected `comment` node, expected `root/)
12081208

12091209
function plugin() {
12101210
/**

0 commit comments

Comments
 (0)