2021-05-15 updated code snipping slightly to show different error possibilities
I am experiencing a problem where I would like to detect if an ArcGIS Server Service is accessible by my app using ArcGIS JavaScript 4x.
Some services are private and require tokens. Handling getting an ArcGIS Token is handled upstream in the app via IdentityManager. However sometimes it doesn't work, or doesn't run for various config reasons.
Before I create a new layer I want to do something like:
const [FeatureLayer, esriRequest] = await loadModules([
"esri/layers/FeatureLayer",
"esri/request"
]);
let layerInfo;
// First lets send a quick request to the service url to make
// sure it exists, and that it doesn't return a 498 token
// required error because we don't have a token.
try {
layerInfo = await esriRequest([myservieURL ending w. ?f=json]);
} catch(err) {
// but in the case that a token is required AND we don't have one,
// we get the cryptic javascript error object:
// details: 'Cannot read property 'trim' of null'
// httpStatus: 200
// Or, if we do like so:
// await esriRequest([serviceURL (no json rest param)], {responseType: "json"})
// we get "failed to fetch" error w httpStatus and message props empty.
console.error(err);
}
The above will work if the service is not private so its not something inherently wrong with the way I am using esriRequests.
Note that if I just open the url in the browser (or just use a vanilla `fetch()` I would get a helpful json response back. Something like:
{"error": {"code": 499,"message": "Token Required","details": []}}
I guess a workaround would be to send a fetch to the url to see if it requires a token and then do a check of my own headers (session vars?) to see if I've got a token. But that seems like a bit of a drag.
Is there a better way that I am missing?