[GIS] show message if map service unavailable

arcgis-javascript-api

If any of my ESRI REST map services are down I want to be able to tell the user, at minimum with an alert or message.

I thought I could use onLayerAddResult(layer, error) but it doesn't seem to be getting called if the layer based on a stopped/down service is added to the map, only when a layer is successfully added.

I see some examples using esri.request but it doesn't seem like I should have to do an extra call to the service since in the Firebug console I see these calls like this to each service:

http:
//myserver/ArcGIS/rest/services/Streets/Mask/MapServer?f=json&dpi=96&transparent=true&format=png8&layers=show%3A-1&callback=dojo.io.script.jsonp_dojoIoScript2._jsonpCallback

With a response of either:

*dojo.io.script.jsonp_dojoIoScript2._jsonpCallback({"serviceDescription"
:
null,"mapName":"Layers","description":"","copyrightText":"","layers":[{"id":0,"name":"LMBCO_Mask","parentLayerId":-1,"defaultVisibility":true,"subLayerIds":null}],"tables":[],"spatialReference":{"wkid":2263},"singleFusedMapCache":false,"initialExtent":{"xmin":806353.163236593,"ymin":139639.306949227,"xmax":1165380.94101437,"ymax":327833.751393672,"spatialReference":{"wkid":2263}},"fullExtent":{"xmin":824755.941014371,"ymin":38597.6402825604,"xmax":1146978.16323659,"ymax":323319.862504783,"spatialReference":{"wkid":2263}},"units":"esriFeet","supportedImageFormatTypes":"PNG32,PNG24,PNG,JPG,DIB,TIFF,EMF,PS,PDF,GIF,SVG,SVGZ,BMP","documentInfo":{"Title":"","Author":"","Comments":"","Subject":"","Category":"","Keywords":"","AntialiasingMode":"Normal","TextAntialiasingMode":"Force"},"capabilities":"Map,Query,Data"});*

if the service is up and running, or:

*dojo.io.script.jsonp_dojoIoScript2._jsonpCallback({"error":{"code":400,"message":"Unable
to complete operation.","details":["Service 'NYCStreets/LMBCO_Mask' of
type 'MapServer' does not exist or is inaccessible."]}});*

if the service is not available.

Is there a way to handle this reponse that is already coming back?

Thanks for your help!

Chad Wilcomb

Best Answer

Try connecting it to a dojo handler, like this:

    dojo.connect(map, "onLayerAddResult", function(layer, error){
    if (error) {
        var content = "Layer could not load: " + layer.id + "<br />" + layer.url + "<br />";
        alert("Major Error loading Content", content);
    }
}); 
Related Question