Send parse error to Promise reject too

This commit is contained in:
Andrey Sitnik
2015-04-24 13:25:55 +03:00
parent 1135d0aad8
commit ef7942aa33
2 changed files with 23 additions and 3 deletions
+15 -2
View File
@@ -24,7 +24,11 @@ export default class LazyResult {
opts.map = { prev: css.map };
}
} else {
root = parse(css, opts);
try {
root = parse(css, opts);
} catch (error) {
this.parseError = error;
}
}
this.result = new Result(processor, root, opts);
@@ -87,7 +91,13 @@ export default class LazyResult {
async() {
if ( this.processed ) {
return Promise.resolve().then( () => this.stringify() );
return new Promise( (resolve, reject) => {
if ( this.parseError ) {
reject(this.parseError);
} else {
resolve(this.stringify());
}
});
}
if ( this.processing ) {
return this.processing;
@@ -95,6 +105,7 @@ export default class LazyResult {
let plugins = this.processor.plugins.slice(0);
this.processing = new Promise( (resolve, reject) => {
if ( this.parseError ) return reject(this.parseError);
this.asyncTick(plugins, resolve, reject);
}).then( () => {
this.processed = true;
@@ -112,6 +123,8 @@ export default class LazyResult {
throw 'Use process(css).then(cb) to work with async plugins';
}
if ( this.parseError ) throw this.parseError;
for ( let plugin of this.result.processor.plugins ) {
let promise = this.run(plugin);
if ( isPromise(promise) ) {
+8 -1
View File
@@ -105,7 +105,7 @@ describe('Processor', () => {
it('throws with file name', () => {
let error;
try {
(new Processor()).process('a {', { from: 'a.css' });
(new Processor()).process('a {', { from: 'a.css' }).css;
} catch (e) {
if ( e instanceof CssSyntaxError ) {
error = e;
@@ -267,6 +267,13 @@ describe('Processor', () => {
});
});
it('throws parse error in async', (done) => {
(new Processor()).process('a{').catch(function (err) {
expect(err.message).to.eql('<css input>:1:1: Unclosed block');
done();
});
});
it('throws error on sync method to async plugin', () => {
let async = () => {
return new Promise( (resolve) => resolve() );