How to suppress javascript errors when xmlhttp.status != 200 - javascript

In my Google Chrome console I keep receiving errors when the xmlhttp status does not result in 200. For example, in certain cases I intentionally set the header of my PHP files to 500 so in javascript I can display the error and avoid anything that requires that PHP file. Since I have my own error handler for this, is there anyway to suppress the default error?
Error Example:
POST http://localhost/mama/cgi-bin/pages/Module-Install.php 500 (Internal server error)

It seems that there is no way to supress these warnings in google chrome when handling the error yourself. For any of those who are looking for an alternate solution, embed the error code in a json object, and parse the json object client side for any errors.

This error was sended by Server side, not browser, not js. You can not just "hide" it from console. You can handle it in onerror event:
request.onerror = function (e) {
// do something to fix the results of server error...
};
In other words, this is a server error and it CAN break your code execution, but if you make couple steps to resume the job after a possible exception from xmlHttp class, the code will be fine.
UPD:
Just if you are pure perfectionist, and you cares about flawless environment:
In a server side you must implement the server answer without a firing any statuses except 200. It's not a super hard job.
For ASP: Suppressing HTTP 500 response codes;
For PHP: http://php.net/manual/en/function.http-response-code.php .

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.

Stop jQuery from console logging on failed XHR GET request

I am trying to load a page when it is done with certain processes. When the process is still running, it returns a 503 error code, when the page is loaded, it returns a 200 code.
I am doing this via the $.get function. However, when jQuery get's a 503 error code (and probably also with other error codes), it logs this in the console:
XHR failed loading: GET "URL".
Example:
How do I remove this console.log()?
The logging entry in chrome's console is a behaviour of chrome when any HTTP request is handled, not a problem with jQuery or ajax(XMLHttpRequest), even an or tag could cause this issue.
So you cannot stop the chrome browser from logging 503 errors in console. Instead you should correct the server side implementation. The server should return 200 even if the process is running, with a response data indicating that the process is running. (You may run your process in background thread on the server asynchronously and return the response on the main thread.)

javascript: how to detect http 4XX errors?

Our website runs a lot of javascripts, I want to capture those client side errors, including javascript errors together with http errors (403 forbidden, 404 not found), and send them back to server via ajax call.
Right now I am able to capture javascript errors with window.onerror function, but for those 403 404 http errors I have no clue, this discussion is very helpful if the missing resource has img tag, and I am able to record it, but is there any more general way to detect 403/404 errors, not just for 'img' tag?
PS: I know that chrome console shows all http errors, but I want to detect it using javascript.
Thanks!

Parsing JSONP Response in Javascript when 4xx or 5xx Http Error Code is Present

I am implementing an application which relies upon communication between a JavaScript client and a server which knows how to respond to the client using JSONP notation.
I am attempting to handle the case in my Javascript client where my server returns with an http status code of 4xx or 5xx. Currently what I'm seeing is that the script is not evaluated as the browser believes it to be an error (which it is.) However, I still want to read what my server has to say in the event of this 4xx or 5xx response code in my JavaScript client.
I'm seeing that this does raise an error on the script tag element, but I'm concerned that this is not cross browser and will not be a robust solution.
Has anyone had any luck on still parsing a jsonp response even though the http status code is 4xx or 5xx?
I'm beginning to believe I should just use this "set a timeout" solution which "detects" a failure by stating the callback function to the jsonp request would complete within a certain time frame, and if it doesn't, there was an error.
EDIT: I'm temporarily always returning 200 status code when my server detects a jsonp client and then tunneling the error message/status in the json object returned. I was hoping to take advantage of the HTTP status codes but I'm thinking that is no-go for a javscript client.
JSONP is a hack to work-around cross-domain issues. When it works, it works well. But when it doesn't you don't have a way to figure out what went wrong.
setTimeout is another hack on top of the original one. If you must use JSONP and still need error detection (not handling), thats what you'd have to do. There isn't a better solution.
If you control the server, try to use alternatives such as Cross-Origin-Resource-Sharing (CORS), or Flash's crossdomain.xml to allow cross domain requests. If you don't control the server, you can proxy the response through your server to get better control.
One approach when using JSONP is to embed status information in the callback. So the callback function signature would look like
callback(result, status, message)
So if your call looks like
http://myurl.com/?callback=fn
generate code for a successful call that looks like
fn({"data":"my great data"}, 200)
and for an exceptional condition
fn(null, 500, "server error"}
You can check the status of the XHR object (if you are not using a JS library).
if(xhr.readyState == 4){
if(xhr.status == 200){
// good
}else if(xhr.status == 502){
// d'oh
}
}
If you are using jQuery, you can pass in a statusCode to handle special cases for $.ajax

Categories

Resources