Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print line number around error in Try-Catch block

My script has several try-catch block which sends email about the error message. It works but only one simple line of error message is sent. What I want is the line number around the error and more descriptive messages to help me identify where the error is happening.

like image 262
Michael SM Avatar asked Dec 05 '12 06:12

Michael SM


1 Answers

You can try this (I stole it somewhere), the first converts all info in the catched exception into a string. The second function can be used to wrap some code and if it throws an excetopn write it somewhere.

function catchToString (err) {
  var errInfo = "Catched something:\n"; 
  for (var prop in err)  {  
    errInfo += "  property: "+ prop+ "\n    value: ["+ err[prop]+ "]\n"; 
  } 
  errInfo += "  toString(): " + " value: [" + err.toString() + "]"; 
  return errInfo;
}
function catched (f) {
  try {
    f ();
  }
  catch(err) { 
    Logger.log (catchToString (err));
  }
}
like image 111
pbhd Avatar answered Oct 13 '22 10:10

pbhd