Permalink
Browse files

fix bug (not throw a exception when fin() exists.)

  • Loading branch information...
1 parent 70e10de commit 19761159a39a2c81f3bf3d03719d1045d4c8a0ab @tasogarepg committed Apr 8, 2012
Showing with 79 additions and 4 deletions.
  1. +1 −0 README.md
  2. +5 −4 lib/node-block.js
  3. +73 −0 test/node-block.test.js
View
@@ -95,6 +95,7 @@ block(
)(sample);
function sample(err){
+ if (err) throw err;
console.log(this.data.d1);
}
```
View
@@ -35,10 +35,11 @@ BlockInfo.prototype.start = function(cb) {
this.stepList.pop();
this.cb = null;
}
- if (cb) {
- this.cb = new StepInfo(this, this.stepList.length, cb);
- this.stepList.push(this.cb);
- }
+ cb = cb || function(err) {
+ if (err) throw err;
+ };
+ this.cb = new StepInfo(this, this.stepList.length, cb);
+ this.stepList.push(this.cb);
this.next(null, 0);
};
View
@@ -129,6 +129,29 @@ describe('node-block', function() {
)(done);
});
+ it('throw exception and async', function(done) {
+ block(
+ function() {
+ this.data.d1 = 'a';
+ throw new Error('test');
+ },
+ function() {
+ assert(false);
+ },
+ function cat(err) {
+ assert.equal(this.data.d1, 'a');
+ assert.equal(err.message, 'test');
+ fs.readFile(fileA, 'utf8', this.async('d2'));
+ fs.readFile(fileB, 'utf8', this.async('d3'));
+ }
+ )(function(err) {
+ var str = this.data.d2 + this.data.d3;
+ assert.equal(str, 'aaaabbbb');
+ assert.equal(err, null);
+ done();
+ });
+ });
+
it('throw exception from async', function(done) {
block(
function() {
@@ -208,6 +231,32 @@ describe('node-block', function() {
});
});
+ it('catch exception and throw async', function(done) {
+ block(
+ function() {
+ throw new Error('test');
+ },
+ function() {
+ assert(false);
+ },
+ function cat(err) {
+ assert.notEqual(err, null);
+ assert.equal(err.message, 'test');
+ this.data.d1 = 'b';
+ fs.readFile(fileA, 'utf8', this.async('d1'));
+ throw err;
+ },
+ function fin(err) {
+ assert.equal(this.data.d1, 'b'); // d1 is not 'aaaa'. It's because cat() throws a err immediately.
+ assert.notEqual(err, null);
+ assert.equal(err.message, 'test');
+ }
+ )(function(err) {
+ assert.notEqual(err, null);
+ done((err.message == 'test') ? null : err);
+ });
+ });
+
it('no catch exception', function(done) {
block(
function() {
@@ -262,6 +311,30 @@ describe('node-block', function() {
});
});
+ it('default callback', function(done) {
+ block(
+ function() {
+ block(
+ function() {
+ throw new Error('test');
+ },
+ function cat(err) {
+ throw err;
+ },
+ function fin() {
+ ;
+ }
+ )();
+ },
+ function() {
+ assert(false);
+ }
+ )(function(err) {
+ assert.notEqual(err, null);
+ done((err.message == 'test') ? null : err);
+ });
+ });
+
it('jump to end', function(done) {
block(
function() {

0 comments on commit 1976115

Please sign in to comment.