Is there a way to catch the errors that are written to the console (e.g unrecognized token) in code? I want to write them to a log of some kind.
You can listen to the window's onerror event.
window.onerror = function(errorMsg, url, lineNumber, charNumber, errObj) {
alert(errObj.message);
};
abc();
This will afaik only be emitted if there is run time errors in loaded files.
You should be able to return false to prevent the original error from showing.
Related
Occasionally, when I run my puppeteer script, I’ll get a “page crashed” error. It’s most likely not a software error, but something to do with the proxy IPs I’m using/webpages I’m scraping. So all I’m really looking to do is just catch the error, close the current page, and open up a new page.
However, my try catch block doesn’t seem to be working. My code is structured like this:
try {
//puppeteer code
}
catch(e) {
console.log(‘caught error ‘ + e);
}
But when the page crashed error happens, I see “unhandled promise rejection error” instead of my console.log message. What’s going on here?
From here:
page.on('error', msg => {
console.error(...);
throw msg ;
});
And this is also advised to globally catch all uncaught errors in your code:
process.on('unhandledRejection', error => {
console.error(...);
throw error;
});
Problem:
I am seeing following error in my browser console, I don't want a solution to resolve this error.
I want a solution to remove from the browser console.
GET https://logo.clearbit.com/objectivepartners.com net::ERR_ABORTED 404
I came to following solution which can handle consoling but while using fetch
it is not working:
console.defaultError = console.error.bind(console);
console.errors = [];
console.error = function(){
if (!arguments[0].includes("404")) {
console.defaultError.apply(console, arguments);
}
console.errors.push(Array.from(arguments));
}
fetch("https://logo.clearbit.com/objectivepartners.com").then(response => {
if (response.ok) {
console.log("okay");
}
}).catch(error => {
console.error("404"); // WILL NOT SHOW IN BROUSER CONSOLE
console.error("error"); // WILL SHOW IN BROWSER CONSOLE
});
Here,
I want if arguments include 404 then do not console it in the browser.
But on the fetch request failure, it includes 404 still it consoling, that I do not want to be happening
could it be possible that when fetch fail i can disable using in this code (by changing) or any other way?
Unfortunately, I didn't find any solution to resolve at the client-side or using javascript.
But I resolve this by creating an endpoint to API side and giving URL to that API to check that URL is valid or not, based on API response I handled in fetch
I looked everywhere but I couldn't find something that helped. This has been really annoying me because I actually tested what I was doing a few weeks ago and it worked then.
So here's my code:
var error_handler = function(e) {
console.error(e);
e.preventDefault();
}
window.addEventListener('error', error_handler);
And that works for handling errors, but it's not handling errors like this:
Failed to load resource: the server responded with a status of 404 ()
when I wrote the code before, it did pick up those errors. Any idea what I'm doing wrong here?
When a resource (such as an <img> or <script>) fails to load, an error event using interface Event is fired at the element that initiated the load, and the onerror() handler on the element is invoked. These error events do not bubble up to window, but can be handled with a EventTarget.addEventListener configured with useCapture set to true.
For more details refer this documentation
Actually, error event does work for resource loading errors, you just need to set useCaputure to true
window.addEventListener("error", (event) => {
console.log('error: ', event);
}, true);
You will not get the status code or text of the network response, but you will have an event that shows you the target that throws the error.
For example:
> error: Event {isTrusted: true, type: 'error', target: img, currentTarget: Window, eventPhase: 1, …}
Loading an external file is not a JavaScript error so it is not going to be caught unless the error comes from the script file from trying to be executed. You can add an error event to the script tag, but you are not going to get the error message from it.
function handleError(evt){
console.log(evt);
}
var scr = document.createElement("script");
scr.src="error.js";
scr.onerror = handleError
document.body.appendChild(scr);
I am trying to catch all JavaScript errors from a specific website via a chrome extension. I am very inexperienced with javascript and for some odd reason my error handler isnt catching all of the errors, most importantly, the one on line 191 "Load timeout for modules":
https://hastebin.com/atuwiboqec.js
The error "script error" on line 507 comes through just fine.
This is my content.js file that I have under "content_scripts" in the manifest, it runs at document_start:
var script=document.createElement("script");
script.src=chrome.runtime.getURL("myscript.js");
script.async=false;
document.documentElement.appendChild(script);
This is my myscript.js file:
window.addEventListener("error", handleException, false);
window.addEventListener("unhandledrejection", handleException, false);
function handleException(I_sMsg) {
console.log("Error0 occured: " + I_sMsg.message);
return cancelEvent(I_sMsg);
};
window.onerror = function ErrorHandler(errorMsg, url, lineNumber) {
console.log("Error1 occured: " + errorMsg);
return false;
};
window.addEventListener("timeout", function(e) {
console.log("Error2 occured: " + e.error.message);
return false;
});
When the error "script error" occurs, both Error0 and Error1 messages get printed. When the error "load timeout for modules" occurs, nothing happens even though the error shows up as "Uncaught Error: Load timeout for modules" in the chrome console, in red, with log level "error". This indicates that the error is being thrown:
error in console image
How can I catch that error, or even better, all errors from that site?
P.S I am unsure of how to trigger that load timeout error. It only happens every now and then. I believe a slow network is the cause.
EDIT: Updated the url domain to a backup one as it wasnt resolving.
I'm trying to check whether an URL returns 404, 403 etc when including a Javascript file. It works ok, but I still get an error in "Chrome developer tools".
This is my code:
(function() {
try
{
var ml = document.createElement('script');
ml.type = 'text/javascript';
ml.async = true;
ml.id = 'monoloop_invoke';
ml.onerror = function(){alert('File does not exist');};
ml.src = 'http://somedomain.com/somefile.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ml, s);
}
catch(err)
{
alert('Error: '+err);
}
})
();
If the file does not exist it shows the error "File does not exist" from the ml.onerror function. This is all good. But the problem is that I still get an error line in my console like:
GET http://somedomain.com/somefile.js 403 (Forbidden)
and the try/catch does not catch this error.
Anyone knows how to solve this? Or is there another way of testing if a URL exists before including it? I cannot use AJAX as I need to use this in a cross-domain fashion. I could use jQuery if necessary.
EDIT: It does not show an error in IE, so i guess this maybe just relates to the way chrome reports issues. Does anyone see a more elegant solution for checking if a file exisists without genreting anything in the console.
jQuery.getScript( url, [ success(data, textStatus) ] )
Description: Load a JavaScript file from the server using a GET HTTP request, then execute it.
url
A string containing the URL to which the request is sent.
success(data, textStatus)
A callback function that is executed if the request succeeds.
To catch the errors use the ajaxError event:
http://api.jquery.com/ajaxError/