Skip to content

Commit 1f76a2e

Browse files
committed
fs: add long stacktrace debugging facility
Enable long stacktraces if NODE_DEBUG=fs is set in the environment. Only applies to the default rethrow callback; it's to help you find places where you forgot to pass in a callback.
1 parent a804347 commit 1f76a2e

3 files changed

Lines changed: 100 additions & 4 deletions

File tree

lib/fs.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,39 @@ var O_WRONLY = constants.O_WRONLY || 0;
5252

5353
var isWindows = process.platform === 'win32';
5454

55-
function rethrow(err) {
56-
if (err) throw err;
55+
var DEBUG = process.env.NODE_DEBUG && /fs/.test(process.env.NODE_DEBUG);
56+
57+
function rethrow() {
58+
// Only enable in debug mode. A backtrace uses ~1000 bytes of heap space and
59+
// is fairly slow to generate.
60+
if (DEBUG) {
61+
var backtrace = new Error;
62+
return function(err) {
63+
if (err) {
64+
backtrace.message = err.message;
65+
err = backtrace;
66+
throw err;
67+
}
68+
};
69+
}
70+
71+
return function(err) {
72+
if (err) {
73+
throw err; // Forgot a callback but don't know where? Use NODE_DEBUG=fs
74+
}
75+
};
5776
}
5877

5978
function maybeCallback(cb) {
60-
return typeof cb === 'function' ? cb : rethrow;
79+
return typeof cb === 'function' ? cb : rethrow();
6180
}
6281

6382
// Ensure that callbacks run in the global context. Only use this function
6483
// for callbacks that are passed to the binding layer, callbacks that are
6584
// invoked from JS already run in the proper scope.
6685
function makeCallback(cb) {
6786
if (typeof cb !== 'function') {
68-
return rethrow;
87+
return rethrow();
6988
}
7089

7190
return function() {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
require('fs').readFile('/'); // throws EISDIR
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
var exec = require('child_process').exec;
25+
var path = require('path');
26+
27+
var callbacks = 0;
28+
29+
function test(env, cb) {
30+
var filename = path.join(common.fixturesDir, 'test-fs-readfile-error.js');
31+
var execPath = process.execPath + ' ' + filename;
32+
var options = { env: env || {} };
33+
exec(execPath, options, function(err, stdout, stderr) {
34+
assert(err);
35+
assert.equal(stdout, '');
36+
assert.notEqual(stderr, '');
37+
cb('' + stderr);
38+
});
39+
}
40+
41+
test({ NODE_DEBUG: '' }, function(data) {
42+
assert(/EISDIR/.test(data));
43+
assert(!/test-fs-readfile-error/.test(data));
44+
callbacks++;
45+
});
46+
47+
test({ NODE_DEBUG: 'fs' }, function(data) {
48+
assert(/EISDIR/.test(data));
49+
assert(/test-fs-readfile-error/.test(data));
50+
callbacks++;
51+
});
52+
53+
process.on('exit', function() {
54+
assert.equal(callbacks, 2);
55+
});

0 commit comments

Comments
 (0)