return obj;
}
public static void logException(Context cx, Scriptable thisObj, java.lang.Object[] args, Function funObj) {
TMLContext context = fetchInitialContext(cx);
// Determine parameters
String msg = null;
Object error = null;
if (args.length == 0) {
context.getlog().error("Unloggable TMLScript error because of too few arguments for logException");
return;
}
else if (args.length == 1) {
msg = "Error in TMLScript";
error = args[0];
}
else {
msg = (String) args[0];
error = args[1];
}
// Determine throwable to log and/or additional message to put out
Throwable throwable = null;
String additionalMsg = null;
if (error instanceof Throwable) {
throwable = (Throwable) error;
}
else if (error instanceof Scriptable) {
Scriptable nativeError = (Scriptable) error;
if (nativeError.has("rhinoException", nativeError)) {
throwable = (RhinoException) ((NativeJavaObject) nativeError.get("rhinoException", nativeError)).unwrap();
}
else if (nativeError.has("javaException", nativeError)) {
throwable = (Throwable) ((NativeJavaObject) nativeError.get("javaException", nativeError)).unwrap();
}
else if (nativeError.has("message", nativeError)) {
additionalMsg = (String) nativeError.get("message", nativeError);
}
else {
additionalMsg = "(scriptable error object without further information: " + nativeError.getClass().getName() + ")";
}
if (nativeError.has("lineNumber", nativeError) && !(throwable instanceof RhinoException)) {
additionalMsg += ". Line number: " + (Integer) nativeError.get("lineNumber", nativeError);
}
}
else {
additionalMsg = "(error object of invalid type: " + error.getClass().getName() + ")";
}
// Try to find job context. If available use the job log to put out error
Logger theLog = context.getlog();
JobContext jobContext = null;
Object jcObj = thisObj.get("jobContext", thisObj);
if (jcObj != null && jcObj instanceof NativeJavaObject) {
jcObj = ((NativeJavaObject) jcObj).unwrap();
if (jcObj instanceof JobContext) {
jobContext = (JobContext) jcObj;
theLog = jobContext.getLog();
}
}
// Put out
if (additionalMsg != null) {
msg += ": " + additionalMsg;
}
if (throwable != null) {
if (throwable instanceof RhinoException) {
RhinoException rhinoEx = (RhinoException) throwable;
if (rhinoEx.lineNumber() != 0) {
msg += ". Line Number " + rhinoEx.lineNumber();
}
if (rhinoEx.lineSource() != null) {
msg += ". Line source: " + rhinoEx.lineSource();
}
}
theLog.error(msg, throwable);
context.addwarning(msg + ". Exception message: " + throwable.getMessage() + ". Exception type: " + throwable.getClass().getName());
}
else {
theLog.error(msg);
context.addwarning(msg);
}
}