diff --git a/src/index.js b/src/index.js index 173f532037..4c424e77ba 100644 --- a/src/index.js +++ b/src/index.js @@ -16,13 +16,25 @@ function normalizeOptions ( options ) { } else { console.warn( warning.message ); // eslint-disable-line no-console } + }, + + onerror: error => { + throw error; } }, options ); } export function compile ( source, _options ) { const options = normalizeOptions( _options ); - const parsed = parse( source, options ); + + let parsed; + + try { + parsed = parse( source ); + } catch ( err ) { + options.onerror( err ); + return; + } const { names } = validate( parsed, source, options ); diff --git a/test/parse.js b/test/parse.js index 0be35e08f9..dc4d8f2e6a 100644 --- a/test/parse.js +++ b/test/parse.js @@ -33,4 +33,23 @@ describe( 'parse', () => { } }); }); + + it( 'handles errors with options.onerror', () => { + let errored = false; + + svelte.compile( `

unclosed`, { + onerror ( err ) { + errored = true; + assert.equal( err.message, `Unexpected end of input` ); + } + }); + + assert.ok( errored ); + }); + + it( 'throws without options.onerror', () => { + assert.throws( () => { + svelte.compile( `

unclosed` ); + }, /Unexpected end of input/ ); + }); });