Browse Source

fs: Missing cb errors are deprecated, not a throw

Commit a804347 makes fs function rethrow errors when the callback is
omitted. While the right thing to do, it's a change from the old v0.8
behavior where such errors were silently ignored.

To give users time to upgrade, temporarily disable that and replace it
with a function that warns once about the deprecated behavior.

Close #5005
v0.10.1-release
isaacs 12 years ago
parent
commit
6bd8b7e540
  1. 24
      lib/fs.js
  2. 11
      test/simple/test-fs-readfile-error.js

24
lib/fs.js

@ -59,22 +59,36 @@ var DEBUG = process.env.NODE_DEBUG && /fs/.test(process.env.NODE_DEBUG);
function rethrow() { function rethrow() {
// Only enable in debug mode. A backtrace uses ~1000 bytes of heap space and // Only enable in debug mode. A backtrace uses ~1000 bytes of heap space and
// is fairly slow to generate. // is fairly slow to generate.
var callback;
if (DEBUG) { if (DEBUG) {
var backtrace = new Error; var backtrace = new Error;
return function(err) { callback = debugCallback;
} else
callback = missingCallback;
return callback;
function debugCallback(err) {
if (err) { if (err) {
backtrace.message = err.message; backtrace.message = err.message;
err = backtrace; err = backtrace;
throw err; missingCallback(err);
} }
};
} }
return function(err) { function missingCallback(err) {
if (err) { if (err) {
if (process.throwDeprecation)
throw err; // Forgot a callback but don't know where? Use NODE_DEBUG=fs throw err; // Forgot a callback but don't know where? Use NODE_DEBUG=fs
else if (!process.noDeprecation) {
var msg = 'fs: missing callback ' + (err.stack || err.message);
if (process.traceDeprecation)
console.trace(msg);
else
console.error(msg);
}
}
} }
};
} }
function maybeCallback(cb) { function maybeCallback(cb) {

11
test/simple/test-fs-readfile-error.js

@ -28,7 +28,7 @@ var callbacks = 0;
function test(env, cb) { function test(env, cb) {
var filename = path.join(common.fixturesDir, 'test-fs-readfile-error.js'); var filename = path.join(common.fixturesDir, 'test-fs-readfile-error.js');
var execPath = process.execPath + ' ' + filename; var execPath = process.execPath + ' --throw-deprecation ' + filename;
var options = { env: env || {} }; var options = { env: env || {} };
exec(execPath, options, function(err, stdout, stderr) { exec(execPath, options, function(err, stdout, stderr) {
assert(err); assert(err);
@ -53,3 +53,12 @@ test({ NODE_DEBUG: 'fs' }, function(data) {
process.on('exit', function() { process.on('exit', function() {
assert.equal(callbacks, 2); assert.equal(callbacks, 2);
}); });
(function() {
console.error('the warnings are normal here.');
// just make sure that this doesn't crash the process.
var fs = require('fs');
fs.readFile(__dirname);
fs.readdir(__filename);
fs.unlink('gee-i-sure-hope-this-file-isnt-important-or-existing');
})();

Loading…
Cancel
Save