admin管理员组文章数量:1332872
How do I get the first line only of a console.log(ex.stack)>
For example I only want this:
TypeError: Object #<Object> has no method 'debug'
Out of this:
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)
How do I get the first line only of a console.log(ex.stack)>
For example I only want this:
TypeError: Object #<Object> has no method 'debug'
Out of this:
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)
Share
Improve this question
asked Jun 14, 2016 at 15:18
HarryHarry
55k76 gold badges186 silver badges270 bronze badges
1 Answer
Reset to default 10If you want the error message, just grab it directly. There's no need to parse it out from the full stack trace:
var Object = {};
try {
Object.debug();
} catch(ex) {
console.log(ex.message);
}
If that's not possible for whatever the reason, the stack trace appears to be nothing but a string:
console.log(typeof ex.stack);
string
... so pick your favourite string manipulation technique:
var Object = {};
try {
Object.debug();
} catch(ex) {
console.log(ex.stack.split("\n", 1).join(""));
}
本文标签: javascriptTry catch exstack get first line onlyStack Overflow
版权声明:本文标题:javascript - Try catch ex.stack get first line only - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742313318a2451315.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论