Skip to content

Commit 99e2a09

Browse files
authoredSep 23, 2017
Merge pull request #539 from tonylukasavage/issue-538
preserve stack traces on async error callbacks
2 parents 7914916 + ebb7417 commit 99e2a09

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed
 

‎bcrypt.js

+24-9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ module.exports.genSaltSync = function genSaltSync(rounds) {
2727
/// @param {Number} [rounds] number of rounds (default 10)
2828
/// @param {Function} cb callback(err, salt)
2929
module.exports.genSalt = function genSalt(rounds, ignore, cb) {
30+
var error;
31+
3032
// if callback is first argument, then use defaults for others
3133
if (typeof arguments[0] === 'function') {
3234
// have to set callback first otherwise arguments are overriden
@@ -47,8 +49,9 @@ module.exports.genSalt = function genSalt(rounds, ignore, cb) {
4749
rounds = 10;
4850
} else if (typeof rounds !== 'number') {
4951
// callback error asynchronously
52+
error = new Error('rounds must be a number');
5053
return process.nextTick(function() {
51-
cb(new Error('rounds must be a number'));
54+
cb(error);
5255
});
5356
}
5457

@@ -87,15 +90,19 @@ module.exports.hashSync = function hashSync(data, salt) {
8790
/// @param {String} salt the salt to use when hashing
8891
/// @param {Function} cb callback(err, hash)
8992
module.exports.hash = function hash(data, salt, cb) {
93+
var error;
94+
9095
if (typeof data === 'function') {
96+
error = new Error('data must be a string and salt must either be a salt string or a number of rounds');
9197
return process.nextTick(function() {
92-
data(new Error('data must be a string and salt must either be a salt string or a number of rounds'));
98+
data(error);
9399
});
94100
}
95101

96102
if (typeof salt === 'function') {
103+
error = new Error('data must be a string and salt must either be a salt string or a number of rounds');
97104
return process.nextTick(function() {
98-
salt(new Error('data must be a string and salt must either be a salt string or a number of rounds'));
105+
salt(error);
99106
});
100107
}
101108

@@ -110,14 +117,16 @@ module.exports.hash = function hash(data, salt, cb) {
110117
}
111118

112119
if (data == null || salt == null) {
120+
error = new Error('data and salt arguments required');
113121
return process.nextTick(function() {
114-
cb(new Error('data and salt arguments required'));
122+
cb(error);
115123
});
116124
}
117125

118126
if (typeof data !== 'string' || (typeof salt !== 'string' && typeof salt !== 'number')) {
127+
error = new Error('data must be a string and salt must either be a salt string or a number of rounds');
119128
return process.nextTick(function() {
120-
cb(new Error('data must be a string and salt must either be a salt string or a number of rounds'));
129+
cb(error);
121130
});
122131
}
123132

@@ -152,15 +161,19 @@ module.exports.compareSync = function compareSync(data, hash) {
152161
/// @param {String} hash expected hash
153162
/// @param {Function} cb callback(err, matched) - matched is true if hashed data matches hash
154163
module.exports.compare = function compare(data, hash, cb) {
164+
var error;
165+
155166
if (typeof data === 'function') {
167+
error = new Error('data and hash arguments required');
156168
return process.nextTick(function() {
157-
data(new Error('data and hash arguments required'));
169+
data(error);
158170
});
159171
}
160172

161173
if (typeof hash === 'function') {
174+
error = new Error('data and hash arguments required');
162175
return process.nextTick(function() {
163-
hash(new Error('data and hash arguments required'));
176+
hash(error);
164177
});
165178
}
166179

@@ -175,14 +188,16 @@ module.exports.compare = function compare(data, hash, cb) {
175188
}
176189

177190
if (data == null || hash == null) {
191+
error = new Error('data and hash arguments required');
178192
return process.nextTick(function() {
179-
cb(new Error('data and hash arguments required'));
193+
cb(error);
180194
});
181195
}
182196

183197
if (typeof data !== 'string' || typeof hash !== 'string') {
198+
error = new Error('data and hash must be strings');
184199
return process.nextTick(function() {
185-
cb(new Error('data and hash must be strings'));
200+
cb(error);
186201
});
187202
}
188203

0 commit comments

Comments
 (0)
Please sign in to comment.