Skip to content

Commit

Permalink
add more useful error message when converting to HTML and no block de…
Browse files Browse the repository at this point in the history
…fintion exists
  • Loading branch information
Ben Briggs committed Jul 30, 2018
1 parent 5184e31 commit dc6e0ca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/convertToHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ const convertToHTML = ({
let closeNestTags = '';
let openNestTags = '';

if (!getBlockHTML(block).nest) {
const blockHTMLResult = getBlockHTML(block);
if (!blockHTMLResult) {
throw new Error(
`convertToHTML: missing HTML definition for block with type ${
block.type
}`
);
}

if (!blockHTMLResult.nest) {
// this block can't be nested, so reset all nesting if necessary
closeNestTags = listStack.reduceRight((string, nestedBlock) => {
return string + getNestedBlockTags(getBlockHTML(nestedBlock)).nestEnd;
Expand Down
13 changes: 13 additions & 0 deletions test/spec/convertToHTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1053,4 +1053,17 @@ describe('convertToHTML', () => {
'<p>👍 <br/><a href="https://www.google.com">Santi Albo</a></p>'
);
});

it('throws a meaningful error when no block definition exists', () => {
const contentState = buildContentState([
{
type: 'test',
text: 'asdf',
},
]);

expect(() => convertToHTML(contentState)).toThrowError(
/missing HTML definition/
);
});
});

0 comments on commit dc6e0ca

Please sign in to comment.