Kmeixner February 2016

How to get detailed error info (line number, filename, etc.) for try-catch in Servoy framework?

I would like to get detailed information from the error object from a try-catch statement, such as the line number and file that reported the error in the Servoy JavaScript framework (https://servoy.com/), how do I go about modifying the code sample below to display this information?

var myObj = {'name':'Bob'}

try {
    var age = myObj.age; // throws error because myObj has no 'age' property
}
catch (e) {
  application.output(e, LOGGINGLEVEL.ERROR); // only outputs message without line number or filename info
}

Answers


Kmeixner February 2016

Servoy's error object contains "lineNumber" and "fileName" properties. It also contains a "rhinoException" property that contains an object with the following methods "getMessage()" and "getScriptStackTrace()".

These properties and messages can be used to get detailed information about the error:

var myObj = {'name':'Bob'}

try {
    var age = myObj.age; // throws error because myObj has no 'age' property
}
catch (e) {
  var iLineNumber = e.lineNumber;
  var sFileName = e.fileName;
  var sMessage = e.rhinoException.getMessage();
  var sStackTrace = e.rhinoException.getScriptStackTrace();
  var sMessage = "The following message was detected at line "+iLineNumber+" of the file "+sFileName+". The error message was: "+sMessage+". The full stack trace was: "+sStackTrace;
  application.output(sMessage, LOGGINGLEVEL.ERROR);
}

Post Status

Asked in February 2016
Viewed 2,785 times
Voted 4
Answered 1 times

Search




Leave an answer