2012-05-10 23 views
18

Khi một ngoại lệ bình thường xảy ra, một vết đống như sau output:Làm thế nào để tạo ra một dấu vết ngăn xếp sâu trong node.js?

util.js:38 
     case '%s': return String(args[i++]); 
         ^
TypeError: Cannot convert object to primitive value 
    at String (unknown source) 
    at util.js:38:25 
    at String.replace (native) 
    at Object.<anonymous> (util.js:35:23) 
    at Object.<anonymous> (console.js:25:36) 
    at EventEmitter.<anonymous> (/project/src/routines/debug/boot.js:16:21) 
    at EventEmitter.emit (/project/node_modules/eventemitter2/lib/eventemitter2.js:319:22) 
    at /project/src/bootstrap.js:15:14 
    at /project/src/util/routineloader.js:36:11 
    at /project/src/util/routineloader.js:47:6 

Đó là rất hữu ích. Khi tôi sau đó làm một nơi nào đó như sau:

process.on('uncaughtException', function(err) { 
     console.trace(); 
     throw err; 
    }); 

tôi chỉ nhận được:

Trace: 
    at EventEmitter.<anonymous> (/project/src/routines/debug/exceptions.js:4:17) 
    at EventEmitter.emit (events.js:88:20) 

Đó là không hữu ích gì cả.

Làm cách nào để làm cho nó trả về toàn bộ dấu vết ngăn xếp, như dấu vết gốc?

+0

thể trùng lặp của (http [làm thế nào để in một vết đống trong Node.js?]: // stackoverflow. com/questions/2923858/how-to-print-a-stack-dấu vết-trong-nút-js) –

Trả lời

25

Bạn sắp thực hiện:

process.on('uncaughtException', function(err) { 
    console.log(err.stack); 
    throw err; 
}); 

function foo() { 
    throw new Error("HI. I'm an error."); 
} 

foo(); 

/* Prints 
Error: HI. I'm an error. 
    at foo (/Users/rye/Desktop/test.js:7:9) 
    at Object.<anonymous> (/Users/rye/Desktop/test.js:10:1) 
    at Module._compile (module.js:441:26) 
    at Object..js (module.js:459:10) 
    at Module.load (module.js:348:31) 
    at Function._load (module.js:308:12) 
    at Array.0 (module.js:479:10) 
    at EventEmitter._tickCallback (node.js:192:40) 
*/ 
2
node --stack_trace_limit=200 your-script.js 
0

Tôi thấy rằng khi tôi sử dụng console.log (err) Tôi đã nhận được kết quả ngay vì vậy tôi nhìn vào api và thấy như sau:

var mystring=require('util').inspect(error_object); 

và bạn sẽ nhận được stack trace như là một chuỗi

Các vấn đề liên quan