From dc6e0ca12c86b408be089747cd5d22177ae0fa12 Mon Sep 17 00:00:00 2001 From: Ben Briggs Date: Mon, 30 Jul 2018 13:56:59 -0400 Subject: [PATCH] add more useful error message when converting to HTML and no block defintion exists --- src/convertToHTML.js | 11 ++++++++++- test/spec/convertToHTML-test.js | 13 +++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/convertToHTML.js b/src/convertToHTML.js index 138aeeb..1cfe589 100644 --- a/src/convertToHTML.js +++ b/src/convertToHTML.js @@ -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; diff --git a/test/spec/convertToHTML-test.js b/test/spec/convertToHTML-test.js index 4b5ff2f..c80d1a9 100644 --- a/test/spec/convertToHTML-test.js +++ b/test/spec/convertToHTML-test.js @@ -1053,4 +1053,17 @@ describe('convertToHTML', () => { '

👍
Santi Albo

' ); }); + + it('throws a meaningful error when no block definition exists', () => { + const contentState = buildContentState([ + { + type: 'test', + text: 'asdf', + }, + ]); + + expect(() => convertToHTML(contentState)).toThrowError( + /missing HTML definition/ + ); + }); });