Skip to content

Commit

Permalink
Merge pull request #199 from sveltejs/onerror
Browse files Browse the repository at this point in the history
intercept parse errors with options.onerror
  • Loading branch information
Rich-Harris authored Dec 15, 2016
2 parents a4a67ff + c453ae5 commit 14ef2ed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
19 changes: 19 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,23 @@ describe( 'parse', () => {
}
});
});

it( 'handles errors with options.onerror', () => {
let errored = false;

svelte.compile( `<h1>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( `<h1>unclosed` );
}, /Unexpected end of input/ );
});
});

0 comments on commit 14ef2ed

Please sign in to comment.