Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
fs: silently swallow error if cb is omitted
Browse files Browse the repository at this point in the history
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.

Fixes #5005.
  • Loading branch information
bnoordhuis committed Mar 13, 2013
1 parent 110cacd commit a9f277e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
41 changes: 39 additions & 2 deletions lib/fs.js
Expand Up @@ -77,16 +77,53 @@ function rethrow() {
};
}

// TODO Replace with rethrow() in v0.12.
var swallow = null;

function maybeCallback(cb) {
return typeof cb === 'function' ? cb : rethrow();
if (typeof cb === 'function') {
return cb;
}

if (typeof swallow !== 'function') {
var backtrace = null;

if (process.noDeprecation !== true) {
// Warn once and give the user a stack trace that points to the callee.
var errmsg = 'Silent fs error swallowing is deprecated and will be ' +
'removed in v0.12';
if (process.throwDeprecation !== true &&
process.traceDeprecation !== true) {
errmsg += '\n' +
'Use --trace-deprecation or --throw-deprecation to get ' +
'a stack trace.';
}
backtrace = new Error(errmsg);
}

swallow = function() {
if (backtrace === null) return;
var err = backtrace;
backtrace = null;
if (process.throwDeprecation) {
throw err;
}
console.error(err.message);
if (process.traceDeprecation) {
console.error(err.stack);
}
};
}

return swallow;
}

// Ensure that callbacks run in the global context. Only use this function
// for callbacks that are passed to the binding layer, callbacks that are
// invoked from JS already run in the proper scope.
function makeCallback(cb) {
if (typeof cb !== 'function') {
return rethrow();
return maybeCallback(null);
}

return function() {
Expand Down
5 changes: 5 additions & 0 deletions test/simple/test-fs-readfile-error.js
Expand Up @@ -24,6 +24,11 @@ var assert = require('assert');
var exec = require('child_process').exec;
var path = require('path');

if (process.versions.node.indexOf('0.10') === 0) {
console.log('Skipping test, fs debug stack traces are disabled in v0.10.');
return;
}

var callbacks = 0;

function test(env, cb) {
Expand Down

0 comments on commit a9f277e

Please sign in to comment.