UNPKG

traceback-safe

Version:

Easy access to the call stack, written in pure JavaScript

94 lines (88 loc) 2.25 kB
// Taken from Node source, deps/v8/src/messages.js // // node: 0.6.2 // v8: 3.6.6.8 exports.FormatStackTrace = FormatStackTrace; 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 = FormatSourcePosition(frame); } 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"); } function FormatSourcePosition(frame) { var fileName; var fileLocation = ""; if (frame.isNative()) { fileLocation = "native"; } else if (frame.isEval()) { fileName = frame.getScriptNameOrSourceURL(); if (!fileName) fileLocation = frame.getEvalOrigin(); } else { fileName = frame.getFileName(); } if (fileName) { fileLocation += fileName; var lineNumber = frame.getLineNumber(); if (lineNumber != null) { fileLocation += ":" + lineNumber; var columnNumber = frame.getColumnNumber(); if (columnNumber) { fileLocation += ":" + columnNumber; } } } if (!fileLocation) { fileLocation = "unknown source"; } var line = ""; var functionName = frame.getFunction().name; var addPrefix = true; var isConstructor = frame.isConstructor(); var isMethodCall = !(frame.isToplevel() || isConstructor); if (isMethodCall) { var methodName = frame.getMethodName(); line += frame.getTypeName() + "."; if (functionName) { line += functionName; if (methodName && (methodName != functionName)) { line += " [as " + methodName + "]"; } } else { line += methodName || "<anonymous>"; } } else if (isConstructor) { line += "new " + (functionName || "<anonymous>"); } else if (functionName) { line += functionName; } else { line += fileLocation; addPrefix = false; } if (addPrefix) { line += " (" + fileLocation + ")"; } return line; }