Suppress Chrome 'Failed to load resource' messages in console - javascript

I'm writing a script that uses an XMLHttpRequest to search for a file defined by a relative path, by attempting to resolve that relative path against other same domain absolute paths that the script is aware of, then attempting to load the file from that resolved url. If I encounter a 404, I just try to resolve the files relative path against another absolute path, and try again. For this particular script, its perfectly fine to encounter a 404- however, my console is littered with 'Failed to load resource: the server responded with a status of 404 (Not Found) messages, and I want to suppress them.
There is no error to catch as far as I can see- error cases are handled by the xmlHttpRequest.onreadystatechange handler, and there is no window.onerror.
Is there any way to suppress these messages?
Thanks

This feature was introduced last year. You can enable it here: DevTools->Settings->General->Console->Hide network messages.
See also Filtering the Console output and Additional settings in the devtools documentation.

Use console.clear() in the catch block or error handler function.
It will clear those request error on the console immediately after it is logged.
PLEASE NOTE
From MDN
Note that in Google Chrome, console.clear() has no effect if the user has selected "Preserve log upon navigation" in the settings.
Read more about it on MDN
try {
var req = new XMLHttpRequest();
req.open('GET', 'https://invalidurl', false);
req.send();
} catch(e) {
console.clear();
}
Should log this
GET https://invalidurl/ net::ERR_NAME_NOT_RESOLVED
but it will be cleared.

If you use a web worker to perform XMLHttpRequest requests, the errors will be logged into the worker script context so they will not appear on the console unless this context will be specifically chosen for display.

Related

How to correctly handle error from fetch() in typescript/javascript? [duplicate]

I'm writing a script that uses an XMLHttpRequest to search for a file defined by a relative path, by attempting to resolve that relative path against other same domain absolute paths that the script is aware of, then attempting to load the file from that resolved url. If I encounter a 404, I just try to resolve the files relative path against another absolute path, and try again. For this particular script, its perfectly fine to encounter a 404- however, my console is littered with 'Failed to load resource: the server responded with a status of 404 (Not Found) messages, and I want to suppress them.
There is no error to catch as far as I can see- error cases are handled by the xmlHttpRequest.onreadystatechange handler, and there is no window.onerror.
Is there any way to suppress these messages?
Thanks
This feature was introduced last year. You can enable it here: DevTools->Settings->General->Console->Hide network messages.
See also Filtering the Console output and Additional settings in the devtools documentation.
Use console.clear() in the catch block or error handler function.
It will clear those request error on the console immediately after it is logged.
PLEASE NOTE
From MDN
Note that in Google Chrome, console.clear() has no effect if the user has selected "Preserve log upon navigation" in the settings.
Read more about it on MDN
try {
var req = new XMLHttpRequest();
req.open('GET', 'https://invalidurl', false);
req.send();
} catch(e) {
console.clear();
}
Should log this
GET https://invalidurl/ net::ERR_NAME_NOT_RESOLVED
but it will be cleared.
If you use a web worker to perform XMLHttpRequest requests, the errors will be logged into the worker script context so they will not appear on the console unless this context will be specifically chosen for display.

Fetch() sends error to console | JavaScript [duplicate]

I'm writing a script that uses an XMLHttpRequest to search for a file defined by a relative path, by attempting to resolve that relative path against other same domain absolute paths that the script is aware of, then attempting to load the file from that resolved url. If I encounter a 404, I just try to resolve the files relative path against another absolute path, and try again. For this particular script, its perfectly fine to encounter a 404- however, my console is littered with 'Failed to load resource: the server responded with a status of 404 (Not Found) messages, and I want to suppress them.
There is no error to catch as far as I can see- error cases are handled by the xmlHttpRequest.onreadystatechange handler, and there is no window.onerror.
Is there any way to suppress these messages?
Thanks
This feature was introduced last year. You can enable it here: DevTools->Settings->General->Console->Hide network messages.
See also Filtering the Console output and Additional settings in the devtools documentation.
Use console.clear() in the catch block or error handler function.
It will clear those request error on the console immediately after it is logged.
PLEASE NOTE
From MDN
Note that in Google Chrome, console.clear() has no effect if the user has selected "Preserve log upon navigation" in the settings.
Read more about it on MDN
try {
var req = new XMLHttpRequest();
req.open('GET', 'https://invalidurl', false);
req.send();
} catch(e) {
console.clear();
}
Should log this
GET https://invalidurl/ net::ERR_NAME_NOT_RESOLVED
but it will be cleared.
If you use a web worker to perform XMLHttpRequest requests, the errors will be logged into the worker script context so they will not appear on the console unless this context will be specifically chosen for display.

Azure Storage Javascript library "createBlobServiceWithSas" throws error: Refused to set unsafe header "user-agent"

Using the Azure Storage JS Client library to upload an image throws an error: "Refused to set unsafe header "user-agent""
All requests in the network tab are 200 or 201, it appears like the xhr requests are working. Is it possible to not set this header or filter it out before the post call? I would like to avoid this error in the console.
https://github.com/Azure/azure-storage-node#azure-storage-javascript-client-library-for-browsers
Have tested the sample azurestoragejs-2.9.100-preview in link you mentioned, it causes no error on my side(both Chrome and Firefox).
Open azure-storage.blob.js lib file, search variable var unsafeHeaders and check whether user-agent is in its list. I saw it on my side and reproduce your problem after deleting it. So it might be missing in your file.
If your lib is unbroken, you can ignore this "error" as nothing goes wrong and it's all implemented by storage lib and browser.
Explanation:
When http request executes, method in this lib will make sure headers in unsafeHeaders list won't be set by xhr. If not, browsers will throw warnings as you have seen, because it's a requirement of xhr standard.
See remarks in this lib.
This check is not necessary, but it prevents warnings from browsers about setting unsafe headers.To be honest I'm not entirely sure hiding these warnings is a good thing, but http-browserify did it, so I will too.
Everyting does work on your side may have proved the check is not necessary. Also in xhr standard, user-agent is no more an unsafe header, but browser doesn't catch up.

How to check for No 'Access-Control-Allow-Origin' header error

When using XMLHttpRequest in JavaScript, is it possible to distinguish between the following two errors (GET completely failed / No 'Access-Control-Allow-Origin' header)?
Obviously, readyState and status of the XMLHttpRequest object don't differ. I tried to use window.onerror to catch all errors but apparently, these two errors do not trigger the callback function.
Your best course of action would be to prevent this error from even occurring in the first place. By making sure the server adds the domain you're trying to access from your script to the Access-Control-Allow-Origin setting you don't have to deal with it client-side.
That said, you should be able to attach a handler to the error event of the XMLHttpRequest object. Using the error message you might be able determine what went wrong; but that is of lesser interest. It doesn't matter to the client what went wrong, only that it isn't going to get it's data, so you can plan accordingly.
var oReq = new XMLHttpRequest();
oReq.addEventListener("error", transferFailed);
oReq.open();
function transferFailed(evt) {
console.log("An error occurred while transferring the file.");
}
More information: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Asynchronous_request

How to make work Chrome extension's uncaught exceptions handler (which doesn't work because of CORS protection)

I set uncaught exceptions handler window.error = function(message, ...) {...} handler inside one of content script JS file of a Chrome extension. Purpose is to catch all such errors inside the extension (content script Javascript files of it) and report them to developers. But instead of real error message and stack trace I always get message "Script error" (occurred at line 1). How can I get real error and stack trace?
Here is what I found to that moment. It is because of CORS protection which by default sanitize the contents of the error if the script in which the error occurred isn't from the same origin as the document that loaded the script. Chrome since version 30 has support of bypassing this protection ("if the script is served with appropriate 'Access-Control-Allow-Origin' headers that
grant the loading document access to the script's contents"). But how can I apply that bypass if content scripts are not served by HTTP but specified in an extension's manifest? For the same reason I can't use crossorigin attribute of <script>.
Also I suspected it can be done through content security police (content_security_policy manifest field) but I couldn't yet.
I realize that answer can be found by reading chromium sources but I hope somebody will tell me it and I will not need read them.
There is no way to do that other than waiting for http://crbug.com/225513 to be fixed

Categories

Resources