This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Improve call-stacks in async fs oprations #3816
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -519,6 +519,76 @@ exports.pump = function(readStream, writeStream, callback) { | |
}); | ||
}; | ||
|
||
// This will bind the current history stack, to the async | ||
// callback error object | ||
exports._bindHistory = function (callback) { | ||
var history = new Error(); | ||
|
||
return function (error) { | ||
// merge history intro error | ||
if (isError(error)) appendHistory(error, history); | ||
|
||
callback.apply(null, arguments); | ||
}; | ||
}; | ||
|
||
function appendHistory(error, history) { | ||
// get the current error.stack handler | ||
var orig = Error.prepareStackTrace; | ||
|
||
// get the history stack list | ||
Error.prepareStackTrace = function (errorObject, stackObject) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're not going to override (i.e. "monkey patch") native functionality. It's not Node's place to do such things. |
||
return stackObject; | ||
}; | ||
var historyStack = history.stack; | ||
|
||
// Include the history stack list in the error | ||
Error.prepareStackTrace = function (errorObject, stackObject) { | ||
var modifiedStack = historyStack.slice(1).concat(stackObject); | ||
|
||
if (typeof orig === 'function') { | ||
return orig(errorObject, modifiedStack); | ||
} else { | ||
return FormatStackTrace(errorObject, modifiedStack); | ||
} | ||
}; | ||
// This will need to be performed, so Error.prepareStackTrace is executed | ||
var errorStack = error.stack; | ||
|
||
// the returned error.stack value is cached, so it is safe to restore | ||
// the error.stack handler | ||
Error.prepareStackTrace = orig; | ||
} | ||
|
||
// copyed from deps/v8/src/messages.js | ||
function FormatStackTrace(error, frames) { | ||
var lines = []; | ||
try { | ||
lines.push(error.toString()); | ||
} catch (e) { | ||
try { | ||
lines.push("<error: " + e + ">"); | ||
} catch (ee) { | ||
lines.push("<error>"); | ||
} | ||
} | ||
for (var i = 0; i < frames.length; i++) { | ||
var frame = frames[i]; | ||
var line; | ||
try { | ||
line = frame.toString(); | ||
} catch (e) { | ||
try { | ||
line = "<error: " + e + ">"; | ||
} catch (ee) { | ||
// Any code that reaches this point is seriously nasty! | ||
line = "<error>"; | ||
} | ||
} | ||
lines.push(" at " + line); | ||
} | ||
return lines.join("\n"); | ||
} | ||
|
||
/** | ||
* Inherit the prototype methods from one constructor into another. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're not going to run
new Error()
every time anything withmakeCallback()
is called. Just not going to happen.