Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fs: added opt-in backtrace

  • Loading branch information...
commit 52b77776b0e686c1e1013951cc4e20145b995ce5 1 parent 912b5e0
@AndreasMadsen authored
Showing with 69 additions and 0 deletions.
  1. +14 −0 lib/fs.js
  2. +55 −0 test/simple/test-fs-backtrace.js
View
14 lib/fs.js
@@ -89,6 +89,20 @@ function makeCallback(cb) {
return rethrow();
}
+ // Only enable in debug mode. A backtrace uses ~1000 bytes of heap space and
+ // is fairly slow to generate.
+ if (DEBUG) {
+ var backtrace = new Error;
+ return function(err) {
+ if (err) {
+ backtrace.stack = err.name + ': ' + err.message +
+ backtrace.stack.substr(backtrace.name.length);
+ err = backtrace;
+ }
+ cb.apply(null, arguments);
+ };
+ }
+
return function() {
return cb.apply(null, arguments);
};
View
55 test/simple/test-fs-backtrace.js
@@ -0,0 +1,55 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+var common = require('../common');
+var assert = require('assert');
+var fs = require('fs');
+var path = require('path');
+var fork = require('child_process').fork;
+
+if (process.argv[2] == 'child') {
+ var missingFile = path.join(common.tmpDir, 'missing.file');
+ var goodStack = /test-fs-backtrace.js/;
+
+ var openFired = false;
+ fs.open(missingFile, 'r', function (err) {
+ openFired = true;
+ assert(goodStack.test(err.stack));
+ });
+
+ var readfileFired = false;
+ fs.readFile(missingFile, function (err) {
+ readfileFired = true;
+ assert(goodStack.test(err.stack));
+ });
+
+ process.once('exit', function () {
+ assert(openFired);
+ assert(readfileFired);
+ });
+} else {
+ var child = fork(__filename, ['child'], {
+ env: { NODE_DEBUG: 'fs' }
+ });
+
+ child.once('close', function (code) {
+ process.exit(code);
+ });
+}
Please sign in to comment.
Something went wrong with that request. Please try again.