Logging Stack Trace of an Exception from Java Extension.
Thingworx does not log the exact exceptions thrown by the Services but rather masks it with intuitive Exceptions like "Unable to Invoke Services",
sometimes the log messages just say "null" not describing the exact origin of the "NullPointerException". Needless to explain how difficult it can get to debug.
Below is a small piece of Java code that can used in Java Extension that will help you log the Stack trace of the Exception.
public static void extractStackTrace(Exception exception, Logger _logger) {
StackTraceElement[] elements = exception.getStackTrace();
String log = null;
for (StackTraceElement element : elements) {
log = (log == null) ? element.toString() : log + ";" + element.toString();
}
_logger.error(log);
}
You can monitor the Exception in the Application log.
It is better if this Stack Track log can be made optional only on Debug Mode.
public static void logError(Exception exception, Logger loggerObject, String message) {
if (exception != null) {
if (isDebugMode) {
extractStackTrace(exception, loggerObject, message);
} else {
loggerObject.error(message + " : " + exception);
}
} else {
loggerObject.error(message);
}
}

