Select to view content in your preferred language

Error handling if basemap is unavailable

1109
2
06-03-2014 08:22 AM
XavierDupessey
Deactivated User
Hello,

I would like to create a map with several ESRI basemaps.
If one basemap is unavailable (for any reason), the application must remain available with the other basemaps.

To simulate an error, I set an invalid URL / token.


  • In this first test, I set valid parameters for the first basemap of the list and invalid parameters for the second basemap. As expected, it is still possible to navigate in the map through the first basemap.


  • [HTML]<!DOCTYPE html>
    <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
      <title></title>

      <link rel="stylesheet" href="http://js.arcgis.com/3.2/js/esri/css/esri.css">
      <script src="http://js.arcgis.com/3.2/"></script>

      <script>
       require(['dojo/on',
         'dojo/topic',
         'dojo/parser',
         'dojo/ready',
         'dojo/domReady!'],
       function (on, topic, parser, ready) {
       
        ready(function () {

         esri.config.defaults.io.timeout = 10000;

         var map = new esri.Map("mapDiv", {
          nav: true,
          slider: true,
          sliderStyle: 'default'
         });

         dojo.connect(map, 'onLayerAddResult', function (result) {
          console.log("onLayerAddResult called");
          console.log(result);
         });

         dojo.connect(map, 'onLayersAddResult', function (results) {
          console.log("onLayersAddResult called");
          console.log(results);
         });

         map.addLayers([
          new esri.layers.ArcGISTiledMapServiceLayer(
           'http://services.esrifrance.fr/arcgis/rest/services/FranceRaster/France_FranceRaster_Premium/MapServer?token=<VALID_TOKEN>',
           { id: 'France_Raster_v4', visible: true, opacity: 1.0 }
          )
         ,
          new esri.layers.ArcGISTiledMapServiceLayer(
           'http://services.esrifrance.fr/arcgis/rest/services/IGN/France_OrthoHR/MapServer?token=<INVALID_TOKEN>',
           { id: 'France_OrthoHR', visible: false, opacity: 1.0 }
          )
         ]);

        });
       
       });
      </script>
    </head>

    <body class="claro" style="background-color: gray;">
      <div id="mapDiv"></div>
    </body>

    </html>
    [/HTML]

  • But in this second test, I set invalid information for the first basemap of the list and valid parameters for the second basemap. Here, the map is not initialized and it is impossible to use it.

  • Moreover, the callback function onLayerAddResult is called only once for the fisrt basemap and the callback function onLayersAddResult is never called.

    [HTML]<!DOCTYPE html>
    <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
      <title></title>

      <link rel="stylesheet" href="http://js.arcgis.com/3.2/js/esri/css/esri.css">
      <script src="http://js.arcgis.com/3.2/"></script>

      <script>
       require(['dojo/on',
         'dojo/topic',
         'dojo/parser',
         'dojo/ready',
         'dojo/domReady!'],
       function (on, topic, parser, ready) {
       
        ready(function () {

         esri.config.defaults.io.timeout = 10000;

         var map = new esri.Map("mapDiv", {
          nav: true,
          slider: true,
          sliderStyle: 'default'
         });

         dojo.connect(map, 'onLayerAddResult', function (result) {
          console.log("onLayerAddResult called");
          console.log(result);
         });

         dojo.connect(map, 'onLayersAddResult', function (results) {
          console.log("onLayersAddResult called");
          console.log(results);
         });

         map.addLayers([
          new esri.layers.ArcGISTiledMapServiceLayer(
           'http://services.esrifrance.fr/arcgis/rest/services/FranceRaster/France_FranceRaster_Premium/MapServer?token=<INVALID_TOKEN>',
           { id: 'France_Raster_v4', visible: false, opacity: 1.0 }
          )
         ,
          new esri.layers.ArcGISTiledMapServiceLayer(
           'http://services.esrifrance.fr/arcgis/rest/services/IGN/France_OrthoHR/MapServer?token=<VALID_TOKEN>',
           { id: 'France_OrthoHR', visible: true, opacity: 1.0 }
          )
         ]);

        });
       
       });
      </script>
    </head>

    <body class="claro" style="background-color: gray;">
      <div id="mapDiv"></div>
    </body>

    </html>
    [/HTML]

Is it by design or is it a bug?
How can I handle errors of the first basemap of the list?

Thank you in advance.

Regards,
0 Kudos
2 Replies
ReneRubalcava
Honored Contributor
I've run into similar error handling situations with the arcgis utils createMap method.
http://forums.arcgis.com/threads/111190-Some-more-useful-error-handling-for-broken-services-please.

In your case though, you may try creating the layer first as a variable then adding it to the map. This will let you listen for errors on the layer.

var layer1 = new esri.layers.ArcGISTiledMapServiceLayer(...);
var layer2 = new esri.layers.ArcGISTiledMapServiceLayer(...);

layer1.on('error', function() {/* handle errors */});
layer2.on('error', function() {/* handle errors */});

map.addLayers([layer1, layer2]);


I haven't tested this, so this could still break the adding of all layers to the map if even one is bad, but you could at least rerun the map.addLayers again with only the valid layers if you needed to.

I'm of the opinion that breaking map creation when 1 out of 2 or more layers added is invalid is a bug, but I haven't heard one way or another. For now, it just boils down to trying to capture and fix errors as they come along.
0 Kudos
KarenRobine
Frequent Contributor
Yes, I agree with Rene's post. It does seem like it needs to be cleaner if one of the layers doesn't exist, or is broken for some reason, there needs to be better error handling. I've tested this theory by setting the esriConfig.defaults.io.timeout setting too small (try setting it to 100ms). I don't see my layers-add-result error event occurring for this case. And I don't see my other error handlers executing. Instead, I'm getting lots of DOJO Script errors. Interesting.

Has anyone found a good way to handle this?
0 Kudos