Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try catch ex.stack get first line only

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)
like image 450
Harry Avatar asked Jun 14 '16 15:06

Harry


1 Answers

If 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(""));
}
like image 186
Álvaro González Avatar answered Nov 17 '22 18:11

Álvaro González