- basic.js
- timers.js
- http.js
- trace
- clarify
const crypto = require('crypto');
const fs = require('fs');
fs.readFile(__filename, function () {
crypto.randomBytes(256, function () {
process.nextTick(function () {
throw new Error('custom error');
});
});
});
function bad() {
throw new Error('custom error');
}
setTimeout(bad, 10);
setTimeout(bad, 20);
const http = require('http');
http.createServer(function () {
throw new Error('custom error');
}).listen(1337, function () {
http.get('http://localhost:1337');
});
$ node --stack_trace_limit=100 -r trace -r clarify wired.js
/trace/showcases/basic.js:7
throw new Error('custom error');
^
Error: custom error
at /trace/showcases/basic.js:7:13
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
/trace/showcases/basic.js:7
throw new Error('custom error');
^
Error: custom error
at /trace/showcases/basic.js:7:13
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
at InternalFieldObject.ondone (/trace/showcases/basic.js:6:13)
at /trace/showcases/basic.js:5:10
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:445:3)
at ReadFileContext.close (fs.js:358:11)
at FSReqWrap.readFileAfterRead [as oncomplete] (fs.js:414:15)
at ReadFileContext.read (fs.js:342:11)
at FSReqWrap.readFileAfterStat [as oncomplete] (fs.js:398:11)
at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:374:11)
at Object.fs.readFile (fs.js:303:11)
at Object.<anonymous> (/trace/showcases/basic.js:4:4)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
at run (bootstrap_node.js:340:7)
at startup (bootstrap_node.js:132:9)
at bootstrap_node.js:455:3
/trace/showcases/basic.js:7
throw new Error('custom error');
^
Error: custom error
at /trace/showcases/basic.js:7:13
/trace/showcases/basic.js:7
throw new Error('custom error');
^
Error: custom error
at /trace/showcases/basic.js:7:13
at InternalFieldObject.ondone (/trace/showcases/basic.js:6:13)
at /trace/showcases/basic.js:5:10
at Object.<anonymous> (/trace/showcases/basic.js:4:4)
/trace/showcases/timers.js:2
throw new Error('custom error');
^
Error: custom error
at Timeout.bad [as _onTimeout] (/trace/showcases/timers.js:2:9)
at tryOnTimeout (timers.js:224:11)
at Timer.listOnTimeout (timers.js:198:5)
/trace/showcases/timers.js:2
throw new Error('custom error');
^
Error: custom error
at Timeout.bad (/trace/showcases/timers.js:2:9)
at tryOnTimeout (timers.js:224:11)
at Timer.listOnTimeout (timers.js:198:5)
at Object.<anonymous> (/trace/showcases/timers.js:5:1)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
at run (bootstrap_node.js:340:7)
at startup (bootstrap_node.js:132:9)
at bootstrap_node.js:455:3
/trace/showcases/timers.js:2
throw new Error('custom error');
^
Error: custom error
at Timeout.bad [as _onTimeout] (/trace/showcases/timers.js:2:9)
/trace/showcases/timers.js:2
throw new Error('custom error');
^
Error: custom error
at Timeout.bad (/trace/showcases/timers.js:2:9)
at Object.<anonymous> (/trace/showcases/timers.js:5:1)
/trace/showcases/http.js:4
throw new Error('custom error');
^
Error: custom error
at Server.<anonymous> (/trace/showcases/http.js:4:9)
at emitTwo (events.js:106:13)
at Server.emit (events.js:191:7)
at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:543:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:105:23)
/trace/showcases/http.js:4
throw new Error('custom error');
^
Error: custom error
at Server.<anonymous> (/trace/showcases/http.js:4:9)
at emitTwo (events.js:106:13)
at Server.emit (events.js:191:7)
at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:543:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:105:23)
at new <anonymous> (_http_common.js:159:16)
at exports.FreeList.alloc (internal/freelist.js:14:46)
at Server.connectionListener (_http_server.js:316:24)
at emitOne (events.js:96:13)
at Server.emit (events.js:188:7)
at TCP.onconnection (net.js:1460:8)
at createServerHandle (net.js:1181:14)
at Server._listen2 (net.js:1225:14)
at listen (net.js:1290:10)
at Server.listen (net.js:1386:5)
at Object.<anonymous> (/trace/showcases/http.js:5:4)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
at run (bootstrap_node.js:340:7)
at startup (bootstrap_node.js:132:9)
at bootstrap_node.js:455:3
/trace/showcases/http.js:4
throw new Error('custom error');
^
Error: custom error
at Server.<anonymous> (/trace/showcases/http.js:4:9)
/trace/showcases/http.js:4
throw new Error('custom error');
^
Error: custom error
at Server.<anonymous> (/trace/showcases/http.js:4:9)
at Object.<anonymous> (/trace/showcases/http.js:5:4)
To activate trace simply add require('trace') to the beginning of your program. clarify works similarly.
require('trace');
require('clarify');
const crypto = require('crypto');
const fs = require('fs');
fs.readFile(__filename, function () {
crypto.randomBytes(256, function () {
process.nextTick(function () {
throw new Error('custom error');
});
});
});
Recent node versions also supports the -r flag, which does the same thing.
To set the number of lines (call sites) in the stack trace set this value. This can be any number (default 10) or Infinity.
Error.stackTraceLimit = 100
This is a build in v8 feature (https://github.com/v8/v8/wiki/Stack-Trace-API) that can also be set using the --stack_trace_limit=100 flag.
You can in theory, but it seriously affect performance. I strongly recommend against it. Trace is useful when you can reproduce a failure, but don't know what lead to that failure.
Please file an issue at https://github.com/AndreasMadsen/trace and I will look into it. It doesn't have to be an isolated test case, but it will of cause take longer for me to debug if it isn't.
Trace is build using AsyncWrap which provides information about the life of async operations (handles). Using this information it can continuously collect and combine stack traces. At the moment AsyncWrap is in development and has known issues, to fix these I've created async-hook.
To interface with the stack trace itself, I've build stack-chain which abstracts all of the compatibility issues. Internally it uses the v8 stack trace API.