10000 Fix: Wrap callback call in try/catch and rethrow async (fixes #45) (#46) · gulpjs/async-done@11fffe0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 11fffe0

Browse files
authored
Fix: Wrap callback call in try/catch and rethrow async (fixes #45) (#46)
1 parent 2b8ad61 commit 11fffe0

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

index.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ var eosConfig = {
1111
error: false,
1212
};
1313

14+
function rethrowAsync(err) {
15+
process.nextTick(rethrow);
16+
17+
function rethrow() {
18+
throw err;
19+
}
20+
}
21+
22+
function tryCatch(fn, args) {
23+
try {
24+
return fn.apply(null, args);
25+
} catch (err) {
26+
rethrowAsync(err);
27+
}
28+
}
29+
1430
function asyncDone(fn, cb) {
1531
cb = once(cb);
1632

@@ -21,18 +37,18 @@ function asyncDone(fn, cb) {
2137
function done() {
2238
d.removeListener('error', onError);
2339
d.exit();
24-
return cb.apply(null, arguments);
40+
return tryCatch(cb, arguments);
2541
}
2642

2743
function onSuccess(result) {
28-
tick(done, null, result);
44+
done(null, result);
2945
}
3046

3147
function onError(error) {
3248
if (!error) {
3349
error = new Error('Promise rejected without Error');
3450
}
35-
tick(done, error);
51+
done(error);
3652
}
3753

3854
function asyncRunner() {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"jscs": "^2.3.5",
4141
"jscs-preset-gulp": "^1.0.0",
4242
"mocha": "^2.4.5",
43+
"pumpify": "^1.3.6",
4344
"rx": "^4.0.6",
4445
"through2": "^2.0.0",
4546
"when": "^3.7.3"

test/promises.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ describe('promises', function() {
4848
var d = domain.create();
4949
d.once('error', function(err) {
5050
expect(err).toExist();
51+
expect(err.message).toContain('Boom');
5152
done();
5253
});
5354
d.run(function() {

test/streams.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var expect = require('expect');
55
var fs = require('fs');
66
var path = require('path');
77
var through = require('through2');
8+
var pumpify = require('pumpify');
89

910
var asyncDone = require('../');
1011

@@ -29,6 +30,21 @@ function failure() {
2930
return read.pipe(new EndStream());
3031
}
3132

33+
function withErr(chunk, _, cb) {
34+
cb(new Error('Fail'));
35+
}
36+
37+
function pumpifyError() {
38+
var read = fs.createReadStream(exists);
39+
var pipeline = pumpify(
40+
through(),
41+
through(withErr),
42+
through()
43+
);
44+
45+
return read.pipe(pipeline);
46+
}
47+
3248
function unpiped() {
3349
return fs.createReadStream(exists);
3450
}
@@ -48,6 +64,14 @@ describe('streams', function() {
4864
});
4965
});
5066

67+
it('should handle an errored pipeline', function(done) {
68+
asyncDone(pumpifyError, function(err) {
69+
expect(err).toBeAn(Error);
70+
expect(err.message).toNotBe('premature close');
71+
done();
72+
});
73+
});
74+
5175
it('handle a returned stream and cb by only calling callback once', function(done) {
5276
asyncDone(function(cb) {
5377
return success().on('end', function() {

0 commit comments

Comments
 (0)
0