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

error-handling, google-apps-script, javascript, try-catch

Solution

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));
  }
}

Problem

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.

Original source