From 7d676712fd75bd57310167b453c33d4e9bcf57ce Mon Sep 17 00:00:00 2001 From: Cory Forsyth Date: Mon, 2 Nov 2015 15:21:13 -0500 Subject: [PATCH] Better error messages when parsing bad mobiledoc fixes #177 --- src/js/parsers/mobiledoc.js | 18 +++++++++++------- tests/unit/editor/editor-test.js | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/js/parsers/mobiledoc.js b/src/js/parsers/mobiledoc.js index f813a7952..53c247404 100644 --- a/src/js/parsers/mobiledoc.js +++ b/src/js/parsers/mobiledoc.js @@ -20,16 +20,20 @@ export default class MobiledocParser { * @return {Post} */ parse({version, sections: sectionData}) { - const markerTypes = sectionData[0]; - const sections = sectionData[1]; + try { + const markerTypes = sectionData[0]; + const sections = sectionData[1]; - const post = this.builder.createPost(); + const post = this.builder.createPost(); - this.markups = []; - this.markerTypes = this.parseMarkerTypes(markerTypes); - this.parseSections(sections, post); + this.markups = []; + this.markerTypes = this.parseMarkerTypes(markerTypes); + this.parseSections(sections, post); - return post; + return post; + } catch (e) { + throw new Error(`Unable to parse mobiledoc: ${e.message}`); + } } parseMarkerTypes(markerTypes) { diff --git a/tests/unit/editor/editor-test.js b/tests/unit/editor/editor-test.js index 3034c5bd4..270de851b 100644 --- a/tests/unit/editor/editor-test.js +++ b/tests/unit/editor/editor-test.js @@ -228,3 +228,21 @@ test('#detectMarkupInRange matching bounds of marker', (assert) => { assert.ok(markup, 'selection has markup'); assert.equal(markup.tagName, 'strong', 'detected markup is strong'); }); + +test('useful error message when given invalid mobiledoc', (assert) => { + let badMobiledoc = { + version: MOBILEDOC_VERSION, + sections: [ + [], + ["incorrect"] + ] + }; + assert.throws(() => { + new Editor({mobiledoc: badMobiledoc}); // jshint ignore:line + }, /unable to parse.*mobiledoc/i); + + let verybadMobiledoc = "not mobiledoc"; + assert.throws(() => { + new Editor({mobiledoc: verybadMobiledoc}); // jshint ignore:line + }, /unable to parse.*mobiledoc/i); +});