Skip to content

Commit

Permalink
Merge pull request #132 from HubSpot/convertToHTML-block-error
Browse files Browse the repository at this point in the history
add more useful error message when unable to convert a block
  • Loading branch information
benbriggs authored Jul 30, 2018
2 parents 5184e31 + dc6e0ca commit db952d9
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 db952d9

Please sign in to comment.