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 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)