How do I catch an image server's exportImage error using the JS API?

221
5
Jump to solution
06-26-2020 01:12 PM
ChristianHoward
New Contributor II

I'm using the 4.X JS API to create client side raster functions for various image services and adding them to the imagery layer's renderingRule property. When I set these raster functions as a renderingRule, an image is usually returned to me as a success. In some cases, I will get an "Unable to load.." error message in my console. I would like to catch this error and use it in my code (display error message, stop loader, etc). However, most methods that I could think of haven't worked out. I've tried the following methods to catch the error:

  • using an AfterInterceptor
  • watching the loaded property on the imagery layer
  • watching the loadError property on the imagery layer
  • try/catch when adding the layer/applying the rule
  • on('layerview-error') event listener
  • layer.when

My suspicion is that the layer "loads" fine with a valid url, so none of these are "failing" because the only failed part is an invalid renderingRule that was sent to the server.

I've created a codepen sample with the bug in place. Open the console and toggle the select to use "GNDVI". Additionally, I've attached a screenshot of the error from my console.

Any help or insight would be appreciated!

1 Solution

Accepted Solutions
Noah-Sager
Esri Regular Contributor

You could try using the error property of the RequestInterceptor. It catches the error, but I'm not sure if this gets you to where you want to go though.

test app

https://codepen.io/noash/pen/GRoMdbL?editors=1001 

View solution in original post

5 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Christian,

   Sure you can catch it like this:

        select.addEventListener('change', function(){
var layer = map.findLayerById('rasterLayer');
map.remove(layer);
var newLayer = new ImageryLayer({
url:"https://landsat2.arcgis.com/arcgis/rest/services/Landsat8_Views/ImageServer",
renderingRule: returnRasterFunction(this.value),
id: 'rasterLayer',
popupTemplate: imagePopupTemplate
});

newLayer.on("layerview-create-error", function(event) {
console.info(event);
//todo do something because of the error now
});
newLayer.on("layerview-create", function(event){
map.add(newLayer);
});
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Reply
0 Kudos
ChristianHoward
New Contributor II

Hey Robert,

Thanks for taking a look at this. Unfortunately, your suggestion does not work. A LayerView is only "created" after the layer is added to the MapView. Neither event listener in your suggestion will fire since the layer being added falls inside the "layerview-create" event listener. I've added both event listeners into my codepen sample (lines 94 - 103) for reference.

When the map.add logic is outside of the "layerview-create" event listener, we'll see the "layerview-create" event fire every time, regardless of whether a valid renderingRule/Raster Function is supplied. I've added console.logs to help illustrate this. I'm 99% sure that the "layerview-create-error" never fires because the layer has a valid URL and that is what is checked during the "create" process. Rendering Rules are purely a visual change, so it seems like that is ignored.

Reply
0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Christian,

   You are right. Sorry I am not sure of a way to catch the error then.

Reply
0 Kudos
Noah-Sager
Esri Regular Contributor

You could try using the error property of the RequestInterceptor. It catches the error, but I'm not sure if this gets you to where you want to go though.

test app

https://codepen.io/noash/pen/GRoMdbL?editors=1001 

View solution in original post

ChristianHoward
New Contributor II

Hey Noah,

That interceptor put me on the right path. I originally tried using the "after" interceptor and not the "error" interceptor. In combination with the "layerview" event listeners, I was able to achieve what I wanted. I've forked your codepen to illustrate this. Thanks!

Now if only I could figure out how to remove those grey triangles around my locked rasters haha