How do I account for a "querynotfound" error?

  • 2
  • Question
  • Updated 1 year ago
I am unable to find out how to set my ajax call to behave differently if the call returns  "querynotfound". How can I set a conditional to catch this particular error? 
I've tried a few things, but I am having a hard time writing a conditional that responds correctly.
Photo of Aaron Walton

Aaron Walton

  • 3 Posts
  • 0 Reply Likes
  • meh

Posted 1 year ago

  • 2
Photo of Gary Perkins

Gary Perkins

  • 3 Posts
  • 1 Reply Like
Aaron, did you ever figure this out? I'm learning jQuery, and I'm using a getJSON call... I can use valid results with dot notation (ie, result.current_observation.weather), but the way the JSON is returned if there's an error, I can't access the error with result.error... it appears to actually be a level higher in the response.

I've been searching Google, with no luck so far. Gonna try a few more hours of searching, might have to try some different logic, but I'd sure like to know the proper way to grab that error code.
Photo of Aaron Walton

Aaron Walton

  • 3 Posts
  • 0 Reply Likes
No, unfortunately, I did not find a solution. After several days of trying, I basically just gave up on the error-handling.
Photo of Gary Perkins

Gary Perkins

  • 3 Posts
  • 1 Reply Like
Good news, I figured it out: 

$.getJSON(url, function(result, stat, err) {
    if (!result.current_observation) {
      alert(err.responseJSON.response.error.description);
    }
    else {

Instead of just passing the result to the callback function, I also pass the status (which in this case is always successful), and the last variable is the jqXHR object. See the getJSON function in the jQuery docs for more information. The short version is that the jqXHR object contains way more information, including errors (or, at least, gives easier access to it).

I'm still just a few notches above layperson when it comes to this, but in any case, the above code basically tests if the current_observation key is present in the object, and if it isn't then report why. 
Photo of Aaron Walton

Aaron Walton

  • 3 Posts
  • 0 Reply Likes
Good job.And thanks for sharing your solution. :)
Photo of Gary Perkins

Gary Perkins

  • 3 Posts
  • 1 Reply Like
You're welcome! I was surprised there were not more questions and answers about this.