如何获取console.log(ex.stack)>
的第一行例如我只想要这个:
TypeError: Object #<Object> has no method 'debug'
出于这个:
TypeError: Object #<Object> has no method 'debug'
at eval at <anonymous> (unknown source)
at eval (native)
at Object._evaluateOn (unknown source)
at Object._evaluateAndWrap (unknown source)
at Object.evaluate (unknown source)
答案 0 :(得分:2)
如果您需要错误消息,只需直接抓取即可。没有必要从完整的堆栈跟踪中解析它:
var Object = {};
try {
Object.debug();
} catch(ex) {
console.log(ex.message);
}
如果由于这个原因无法实现,那么堆栈跟踪似乎只是一个字符串:
console.log(typeof ex.stack);
的字符串
...所以选择你最喜欢的字符串操作tecnique:
var Object = {};
try {
Object.debug();
} catch(ex) {
console.log(ex.stack.split("\n", 1).join(""));
}