How do I handle a service URL that returns an HTML page with a HTTP 200 code

2085
3
Jump to solution
08-18-2016 01:46 PM
BryanBoutz1
New Contributor III

Occasionally one of the services used by my application but that I don't control fails and starts returning HTML content with a 200 HTTP code rather than any error code or even a valid JSON response with an error.  The specific scenario hanging me up is that the web adaptor server putting up an error page instead of passing through to the map service, but I've also seen it happen when an overzealous security rule blocks a request.  For my purposes, I am testing this by pointing a map to a static html page.

When this happens, I have just called map.addLayers with the bad layer included. The console then shows "init.js:182 SyntaxError: Unexpected token <"  and it appears the problem is Dojo trying to parse JSON. (Stack trace is below).  What really hurts is that this then stops the map from firing the onLayersAddResult event so I am not able to do additional application setup tasks that are needed after the map loads.  To be fair, I haven't tried using on("Layers-Add-Result") or adding a single layer at a time as that would require significant rework of my app.

I have a few ugly ideas for work-arounds, but I'm hoping someone has a more elegant suggestion. I should say, this is using the 3.14 API, but I got the same results from the current 3.17 version.

Stack trace from console:
init.js:182 SyntaxError: Unexpected token <

at Object.b.fromJson (http://js.arcgis.com/3.14/init.js:220:426)
at Object.json (http://js.arcgis.com/3.14/init.js:177:392)
at s (http://js.arcgis.com/3.14/init.js:182:81)
at c (http://js.arcgis.com/3.14/init.js:66:221)
at d (http://js.arcgis.com/3.14/init.js:66:10)
at resolve.callback (http://js.arcgis.com/3.14/init.js:67:350)
at http://js.arcgis.com/3.14/init.js:185:300
at k (http://js.arcgis.com/3.14/init.js:189:431)
at m (http://js.arcgis.com/3.14/init.js:189:357)
at f.resolve (http://js.arcgis.com/3.14/init.js:191:441)

0 Kudos
1 Solution

Accepted Solutions
BryanBoutz1
New Contributor III

My final solution to this problem was to add a timer event set for a bit longer than a typical service timeout that called the same functions as onLayersAddResult if the normal event didn't fire before it. The user has a long wait, but eventually the application continues operating mostly normally. This is only to deal with odd error conditions on services, so normally that fall back will not be used.  Interestingly, when I deployed to the customer, I couldn't replicate the no event firing problem, so at this point I don't know if the work around will ever actually be needed.

View solution in original post

0 Kudos
3 Replies
BryanBoutz1
New Contributor III

Upon further observation, this syntax Error doesn't seem to be the problem. Eventually a timeout passes and the callback does fire.  I swear it didn't do that yesterday, but for now this is not the right question to be asking.

0 Kudos
BryanBoutz1
New Contributor III

After additional struggle, I have found that onLayersAddResult is still not running for Internet Explorer in most cases.  However, if the F12 developer tools are running and a break point is set in the function that adds the layers it will almost always fire the event.  So it appears the IE javascript engine is choking on an exception otherwise. I continue to look for a work around.

0 Kudos
BryanBoutz1
New Contributor III

My final solution to this problem was to add a timer event set for a bit longer than a typical service timeout that called the same functions as onLayersAddResult if the normal event didn't fire before it. The user has a long wait, but eventually the application continues operating mostly normally. This is only to deal with odd error conditions on services, so normally that fall back will not be used.  Interestingly, when I deployed to the customer, I couldn't replicate the no event firing problem, so at this point I don't know if the work around will ever actually be needed.

0 Kudos