Skip to content

Commit

Permalink
Pipe uncought parse errors (e.g.: no onError defined) to the browse…
Browse files Browse the repository at this point in the history
…r console. Fixes #4.
  • Loading branch information
developit committed May 25, 2016
1 parent e708952 commit 50e049b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ export default class Markup extends Component {

try {
vdom = markupToVdom(markup, type, h, this.map, options);
if (onError) onError({ error });
} catch (error) {
if (onError) {
onError({ error });
}
else if (typeof console!=='undefined' && console.error) {
console.error(`preact-markup: ${error}`);
}
}

if (wrap===false) return vdom && vdom[0] || null;
Expand Down
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,16 @@ describe('Markup', () => {
element.dispatchEvent(ev);
});
});

it('should pipe parse errors to console', () => {
sinon.spy(console, 'error');

let invalidXml = `<h1>Test with & symbol</h1>`;

render(<Markup markup={invalidXml} />, scratch);

expect(console.error)
.to.have.been.calledOnce
.and.calledWithMatch('preact-markup: Error: error on line 2 at column 21: xmlParseEntityRef: no name');
});
});

0 comments on commit 50e049b

Please sign in to comment.