Skip to content

Commit

Permalink
Fix: Wrap callback call in try/catch and rethrow async (fixes #45) (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated authored Jan 15, 2018
1 parent 2b8ad61 commit 11fffe0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
22 changes: 19 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ var eosConfig = {
error: false,
};

function rethrowAsync(err) {
process.nextTick(rethrow);

function rethrow() {
throw err;
}
}

function tryCatch(fn, args) {
try {
return fn.apply(null, args);
} catch (err) {
rethrowAsync(err);
}
}

function asyncDone(fn, cb) {
cb = once(cb);

Expand All @@ -21,18 +37,18 @@ function asyncDone(fn, cb) {
function done() {
d.removeListener('error', onError);
d.exit();
return cb.apply(null, arguments);
return tryCatch(cb, arguments);
}

function onSuccess(result) {
tick(done, null, result);
done(null, result);
}

function onError(error) {
if (!error) {
error = new Error('Promise rejected without Error');
}
tick(done, error);
done(error);
}

function asyncRunner() {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"jscs": "^2.3.5",
"jscs-preset-gulp": "^1.0.0",
"mocha": "^2.4.5",
"pumpify": "^1.3.6",
"rx": "^4.0.6",
"through2": "^2.0.0",
"when": "^3.7.3"
Expand Down
1 change: 1 addition & 0 deletions test/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('promises', function() {
var d = domain.create();
d.once('error', function(err) {
expect(err).toExist();
expect(err.message).toContain('Boom');
done();
});
d.run(function() {
Expand Down
24 changes: 24 additions & 0 deletions test/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var expect = require('expect');
var fs = require('fs');
var path = require('path');
var through = require('through2');
var pumpify = require('pumpify');

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

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

function withErr(chunk, _, cb) {
cb(new Error('Fail'));
}

function pumpifyError() {
var read = fs.createReadStream(exists);
var pipeline = pumpify(
through(),
through(withErr),
through()
);

return read.pipe(pipeline);
}

function unpiped() {
return fs.createReadStream(exists);
}
Expand All @@ -48,6 +64,14 @@ describe('streams', function() {
});
});

it('should handle an errored pipeline', function(done) {
asyncDone(pumpifyError, function(err) {
expect(err).toBeAn(Error);
expect(err.message).toNotBe('premature close');
done();
});
});

it('handle a returned stream and cb by only calling callback once', function(done) {
asyncDone(function(cb) {
return success().on('end', function() {
Expand Down

0 comments on commit 11fffe0

Please sign in to comment.