20

So on our site we have various searches some of which work fine and return the appropriate results. A few of them however return the javascript error:

Failed to load resource: net::ERR_BLOCKED_BY_CLIENT when I search on my machine.

I have found out that issue is that I am running AdBlocker in Google Chrome and it is AdBlocker that is causing the problem. Now I know I could just turn AdBlocker off which is fine and I have, but is there a way for me to catch this error in javascript and report to the user why they are not getting any search results?

Ideally I am after something similar to a c# try/catch.

EDIT: OK, so after some digging around and being pointed in the right direction from the comments below I think I have deduced the issue, hopefully this will help others.

After having read this it looks like what I am trying to accomplish cannot be done using the version of jQuery we are currently running (1.10.x) so I guess the solution is to use a new version of jQuery (2.x) and see if I can catch the error

Community
  • 1
  • 1
ltw1991
  • 325
  • 1
  • 2
  • 12
  • Possible duplicate of: http://stackoverflow.com/questions/27277656/how-to-handle-the-err-blocked-by-client-using-javascript As this occurs with network requests, you can detect a failure if it's an XHR. If it's an asset then it's a bit more difficult. This is Chrome blocking some request by instruction. – Perry Mitchell Nov 10 '16 at 12:19
  • So it's not strictly XHR, it's using $.getJSON() to send the request and retrieve the results. I had a look here: http://stackoverflow.com/questions/1740218/error-handling-in-getjson-calls And it looks like I am going to have to change how the request is sent... – ltw1991 Nov 10 '16 at 12:46
  • But that link shows that you can handle errors (http://stackoverflow.com/a/5553540/966338) with `.error(function() { alert("error"); })`.. Does that not work for you in this case? – Perry Mitchell Nov 11 '16 at 08:55
  • If you're looking to catch that error specifically from within the browser - unfortunately it's not going to be possible. You'll only ever get a **generic error message**. – Perry Mitchell Nov 11 '16 at 09:02
  • Unfortunately not, I have tried `.error(function() { alert('error'); });` and `.onerror(function() { alert('error'); });` Both return the javascript error not a function – ltw1991 Nov 11 '16 at 09:51
  • @PerryMitchell Actually `.error(function() { });` does not return a javascript error but the alert does not fire – ltw1991 Nov 11 '16 at 10:03
  • Not exactly an answer to your question, but what might help is detecting when offline and then not making requests until online: `window.addEventListener("offline", () => { console.log("on no, offline!") });` – Elijah Mock Nov 29 '20 at 23:21
  • 1
    Somewhat off topic, but I was surprised to find out that bug notification systems like Bugsnag and Sentry are blocked by Ad Blockers like uBlock Origin, etc. That sucks. – Joshua Pinter Mar 11 '21 at 16:06

2 Answers2

10

Unfortunately you cannot catch that error message specifically, but you can catch the error itself:

$.ajax({
  url: 'http://openx.net',
  dataType: 'json',
  success: function( data ) {
    console.log( "Success:", data);
  },
  error: function( data ) {
    console.log( "Error:", data);
  }
});

Chrome blocking ad call

Obviously the example isn't requesting JSON, but you can see that it fails and calls the error handler.

These errors are fired by Chrome when, for instance, a plugin like Adblock (as you mentioned) cancels a request.

Perry Mitchell
  • 665
  • 6
  • 16
  • I have tried this but I don't get anything output to the console: `$.ajax({ type: 'GET', url: url, contentType: 'application/json', dataType: 'json', success: function (data) { //do stuff }, error: function(err) { console.log("Error:", err); } });` – ltw1991 Nov 11 '16 at 14:00
  • I've got it working with [jsbin here](http://jsbin.com/hafunepoxa/edit?js,output). Make sure ad block is on, and the error will appear in the console. – Perry Mitchell Nov 11 '16 at 14:12
  • 1
    I am gunna mark this as answered as your link works for me, I am clearly missing something in my code hopefully I will find out what it is! Thank you though! – ltw1991 Nov 11 '16 at 14:18
  • Please note: this won't work with jQuery 1.x : http://stackoverflow.com/questions/42159558/how-detect-handle-neterr-blocked-by-client-with-jquery-1-x – Dr. Gianluigi Zane Zanettini Feb 10 '17 at 12:53
  • **net::ERR_CONNECTION_TIMED_OUT** is different, not work a console response. – KingRider Jan 10 '18 at 13:22
  • @KingRider My example specifically shows ERR_BLOCKED_BY_CLIENT - I didn't mention ERR_CONNECTION_TIMED_OUT at all, and can't say if it'd ever work for that specific error. – Perry Mitchell Jan 23 '18 at 07:03
0

*For those like to me trying to get around it....

If you have any Ad Blockers, disable them for the URL.

El_boogy
  • 145
  • 4