Search code examples
jqueryjsonapidictionarygeonames

Jquery error function to return exception message


I have an auto-complete input where visitors enter a location and it provides suggestions while you type. I'm using the Geonames web server for this.

I'm trying to return an exception message if an error occurs on the Geonames web server. For example if I exceed my web server request limit I want the script to send me an email alert.

The exception message returned is JSON.

This is part of the script with the error() function. I deliberately made a typo in the username parameter in data to get an error but no alert appears.

$(function() {
        $( "#city" ).autocomplete({
            source: function( request, response ) {
                $.ajax({
                    url: "http://api.geonames.org/searchJSON",
                    dataType: "jsonp",
                    data: {
                        q: request.term,
                        countryBias: "US",
                        featureClass: "P",
                        style: "full",
                        maxRows: 10,
                        ussername: "demo",
                        operator: "OR"
                    },
                    success: function( data ) {
                        response( $.map( data.geonames, function( item ) {                                                           
                            return {
                                label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                                value: item.name,
                                saved_country: item.countryName,
                                saved_state: item.adminName1,
                                saved_city: item.name,
                                saved_zipcode: item.postalcode,
                                saved_latitude: item.lat,
                                saved_longitude: item.lng,                                                                                              
                            }
                        }));  
                    },  
                    error: function( data ) {
                        response( $.map( data.geonames, function( item ) {                                                           
                            return {
                                request_status: item.status                             
                            }
                            alert("Error msg!");
                        }));  
                    },                                              
                });             
            },

Solution

  • Geonames will return this error information in the success hundler, not the error handler. So you'd have to ask inside your success function:

    success: function( data ) {
        if(typeof(data.status) != 'undefined'){ //It means there was an error
           var errorObject = data;
           //Now you have access to errorObject.status, errorObject.status.message and so on
           //Do something with your error object
           return; //Avoid execution of the rest of the code of this function
        } //Can add an else here if you want
        response( $.map( data.geonames, function(item){
          ...
    
    }
    

    Hope this helps. Cheers