Skip to content

Commit db952d9

Browse files
authored
Merge pull request #132 from HubSpot/convertToHTML-block-error
add more useful error message when unable to convert a block
2 parents 5184e31 + dc6e0ca commit db952d9

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/convertToHTML.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,16 @@ const convertToHTML = ({
5050
let closeNestTags = '';
5151
let openNestTags = '';
5252

53-
if (!getBlockHTML(block).nest) {
53+
const blockHTMLResult = getBlockHTML(block);
54+
if (!blockHTMLResult) {
55+
throw new Error(
56+
`convertToHTML: missing HTML definition for block with type ${
57+
block.type
58+
}`
59+
);
60+
}
61+
62+
if (!blockHTMLResult.nest) {
5463
// this block can't be nested, so reset all nesting if necessary
5564
closeNestTags = listStack.reduceRight((string, nestedBlock) => {
5665
return string + getNestedBlockTags(getBlockHTML(nestedBlock)).nestEnd;

test/spec/convertToHTML-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,4 +1053,17 @@ describe('convertToHTML', () => {
10531053
'<p>👍 <br/><a href="https://www.google.com">Santi Albo</a></p>'
10541054
);
10551055
});
1056+
1057+
it('throws a meaningful error when no block definition exists', () => {
1058+
const contentState = buildContentState([
1059+
{
1060+
type: 'test',
1061+
text: 'asdf',
1062+
},
1063+
]);
1064+
1065+
expect(() => convertToHTML(contentState)).toThrowError(
1066+
/missing HTML definition/
1067+
);
1068+
});
10561069
});

0 commit comments

Comments
 (0)