Один из способов сделать это - переписать console.error
и console.warn
с вашей собственной пользовательской реализацией, поэтому всякий раз, когда какая-либо часть кода вызывает console.error
или console.warn
, вызов будет перехватываться вашей пользовательской функцией. где вы можете выполнить необходимые действия над ним.
В следующем примере показана пользовательская реализация метода console.error
.
//Store the original reference of console.error
var orgError = console.error;
//Overwirte the default function
console.error = function(error) {
//All error will be intercepted here
alert("Intercepted -> " + error);
//Invoke the original console.error to show the message in console
return orgError(error);
}
try {
//Following line will throw error
nonExistentFunction();
} catch (error) {
console.error(error);
}