JS 3.x, set a 'global' error trapping or error event listener

171
0
12-31-2020 07:50 AM
AZendel
Occasional Contributor III

I have an app written against the 3.21 ArcGIS Javascript API. It allows users to select a number of parcels. Then the user clicks a button that automatically fills in several text boxes with attributes from various zones that the parcels are located in, such as voting precincts, city council districts, planning zones, utility districts, etc. I'm successfully accomplishing this by executing a series of QueryTask() operation, such as 

//User clicks the button to automatically fill in the form.

QueryZoning();
QueryZoningOverlay();
QueryCouncilDistrict();
//.... there are about 15 of these

function QueryZoning(){
   var zoneQuery = new esri.tasks.Query();
   var zoneQueryTask = new esri.tasks.QueryTask("https://our.org/arcgis/rest/services/maps/zone/MapServer/0");
   zoneQuery.geometry = queryPolygon;
   zoneQueryTask.execute(zoneQuery, handleZoneOverlayQueryResults);
}
function handleZoneOverlayQueryResults(results) {
        if (!(results.features === undefined || results.features.length == 0)) {
            results.features.forEach(function(feature) {
                zoningOutputString = feature.attributes.ZONE_TYPE );
            });
        } 
        //code that fills in the text box with zoningOutputString goes here
}

I make about 15 calls of this nature - and it's working well....until an error happens in the a function that sets up the QueryTask or the call back function that handles the query. When one of these unhandled errors occurs, the app becomes unresponsive. I realize that I could add try/catch statements to each function that creates the QueryTask and each function that handles the response, but I'd have to do that in about 30 separate functions. There has to be a better and more efficient way to somehow 'globally' handle an error in any of these functions. I have tried:

window.onerror = function(errMsg, url, line, column, error) {.....}
//also tried:
window.addEventListener('error', function (e) {
   var error = e.error;
   console.log("az" + error);
});

But these 'events' and 'error  handlers' do not get triggered when an error occurs. And that makes it hard for me to notify the user that an error occurred and reset the form so it is no longer unresponsive.

How can I trap these errors without having to use 30+ try/catch blocks?  Thanks in advance for any insight you can provide!

 

0 Replies