I have a mobile app that uses cordova + AngularJS 1.2.4 It works perfect on every platform I tested except some Android 2.3.X devices throw a mysterious illegal access
exception. Below is the portion of code that raises the error. Precisely speaking, the line return logFn.apply(console, args);
seems to be the culprit. If I remove the parameter args
code works fine. It seems odd since variable args
is defined at the top of the function. I don't understand what is really causing this illegal access
error? Any one had a similar problem? Any ideas how to fix it?
ps: Please check out inline comments to better understand the problem. There, I debugged and reported some cases.
function consoleLog(type) {
var console = $window.console || {},
logFn = console[type] || console.log || noop;
if (logFn.apply) {
return function() {
var args = [];
forEach(arguments, function(arg) {
args.push(formatError(arg));
// console.log(typeof arg); -> prints string
// console.log(arg.toString); -> prints function
// console.log(arg); -> throws illegal access error
// console.log(arg.toString()); -> throws illegal access error
});
// console.log(args); -> throws illegal access error
// return logFn.apply(console); -> works fine w/o `args`
return logFn.apply(console, args); -> throws illegal access error
};
}
// we are IE which either doesn't have window.console => this is noop and we do nothing,
// or we are IE where console.log doesn't have apply so we log at least first 2 args
return function(arg1, arg2) {
logFn(arg1, arg2 == null ? '' : arg2);
};
}