Skip to content

Commit

Permalink
Better error messages when parsing bad mobiledoc
Browse files Browse the repository at this point in the history
fixes #177
  • Loading branch information
bantic committed Nov 2, 2015
1 parent 28c57d7 commit 7d67671
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/js/parsers/mobiledoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/editor/editor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 7d67671

Please sign in to comment.