<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: esrRequest does not handle missing token in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/esrrequest-does-not-handle-missing-token/m-p/1057664#M73100</link>
    <description>&lt;P&gt;Sorry, don't know what I was thinking, missed that. You might be able to get a better detailed message with an interceptor.&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-config.html#RequestInterceptor" target="_blank"&gt;https://developers.arcgis.com/javascript/latest/api-reference/esri-config.html#RequestInterceptor&lt;/A&gt;&lt;/P&gt;&lt;P&gt;We typically expose this on the layer in various load errors/warnings&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#loadError" target="_blank"&gt;https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#loadError&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Not sure why it's not caught properly in a catch&lt;/P&gt;</description>
    <pubDate>Thu, 13 May 2021 16:10:42 GMT</pubDate>
    <dc:creator>ReneRubalcava</dc:creator>
    <dc:date>2021-05-13T16:10:42Z</dc:date>
    <item>
      <title>esrRequest does not handle missing token</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/esrrequest-does-not-handle-missing-token/m-p/1056924#M73050</link>
      <description>&lt;P&gt;&lt;EM&gt;2021-05-15 updated code snipping slightly to show different error possibilities&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Some services are private and require tokens. Handling getting an ArcGIS Token is handled upstream in the app via &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-identity-IdentityManager.html" target="_self"&gt;IdentityManager&lt;/A&gt;. However sometimes it doesn't work, or doesn't run for various config reasons.&lt;/P&gt;&lt;P&gt;Before I create a new layer I want to do something like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;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); 
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;The above will work if the service is not private so its not something inherently wrong with the way I am using &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-request.html" target="_self"&gt;esriRequests.&amp;nbsp;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Note that if I just open the url in the browser (or just use a vanilla `fetch()`&amp;nbsp; I would get a helpful json response back. Something like:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN class="b"&gt;{&lt;/SPAN&gt;&lt;SPAN class="blockInner"&gt;&lt;SPAN class="kvov objProp"&gt;"&lt;SPAN class="k"&gt;error&lt;/SPAN&gt;":&amp;nbsp;&lt;SPAN class="b"&gt;{&lt;/SPAN&gt;"&lt;SPAN class="k"&gt;code&lt;/SPAN&gt;":&amp;nbsp;&lt;SPAN class="n"&gt;499&lt;/SPAN&gt;,"&lt;SPAN class="k"&gt;message&lt;/SPAN&gt;":&amp;nbsp;&lt;SPAN class="s"&gt;"&lt;SPAN&gt;Token Required&lt;/SPAN&gt;"&lt;/SPAN&gt;,"&lt;SPAN class="k"&gt;details&lt;/SPAN&gt;":&amp;nbsp;&lt;SPAN class="b"&gt;[&lt;/SPAN&gt;&lt;SPAN class="b"&gt;]&lt;/SPAN&gt;&lt;SPAN class="b"&gt;}&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class="b"&gt;}&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN class="b"&gt;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.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Is there a better way that I am missing?&lt;/P&gt;</description>
      <pubDate>Sat, 15 May 2021 20:27:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/esrrequest-does-not-handle-missing-token/m-p/1056924#M73050</guid>
      <dc:creator>RyanSutcliffe</dc:creator>
      <dc:date>2021-05-15T20:27:35Z</dc:date>
    </item>
    <item>
      <title>Re: esrRequest does not handle missing token</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/esrrequest-does-not-handle-missing-token/m-p/1057197#M73067</link>
      <description>&lt;P&gt;If you add a .catch() to the promise chain will you get the error that a token is required?&lt;/P&gt;</description>
      <pubDate>Wed, 12 May 2021 16:08:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/esrrequest-does-not-handle-missing-token/m-p/1057197#M73067</guid>
      <dc:creator>ReneRubalcava</dc:creator>
      <dc:date>2021-05-12T16:08:23Z</dc:date>
    </item>
    <item>
      <title>Re: esrRequest does not handle missing token</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/esrrequest-does-not-handle-missing-token/m-p/1057393#M73085</link>
      <description>&lt;P&gt;Not sure what you mean. Is there a different way you'd write the try/catch statement above? I can confirm that the async/await works properly and catches the error from await esriRequest.&lt;/P&gt;</description>
      <pubDate>Wed, 12 May 2021 21:49:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/esrrequest-does-not-handle-missing-token/m-p/1057393#M73085</guid>
      <dc:creator>RyanSutcliffe</dc:creator>
      <dc:date>2021-05-12T21:49:33Z</dc:date>
    </item>
    <item>
      <title>Re: esrRequest does not handle missing token</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/esrrequest-does-not-handle-missing-token/m-p/1057664#M73100</link>
      <description>&lt;P&gt;Sorry, don't know what I was thinking, missed that. You might be able to get a better detailed message with an interceptor.&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-config.html#RequestInterceptor" target="_blank"&gt;https://developers.arcgis.com/javascript/latest/api-reference/esri-config.html#RequestInterceptor&lt;/A&gt;&lt;/P&gt;&lt;P&gt;We typically expose this on the layer in various load errors/warnings&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#loadError" target="_blank"&gt;https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#loadError&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Not sure why it's not caught properly in a catch&lt;/P&gt;</description>
      <pubDate>Thu, 13 May 2021 16:10:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/esrrequest-does-not-handle-missing-token/m-p/1057664#M73100</guid>
      <dc:creator>ReneRubalcava</dc:creator>
      <dc:date>2021-05-13T16:10:42Z</dc:date>
    </item>
    <item>
      <title>Re: esrRequest does not handle missing token</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/esrrequest-does-not-handle-missing-token/m-p/1058301#M73121</link>
      <description>&lt;P&gt;Thanks, I tried testing setting up a custom interceptor. Something like:&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;esriConfig.request.interceptors.push({
    urls: "[mygisserver domain url www.example.com/]",
    before: function(params) {
      if (params) {
        console.log(params)
      }
    },
    after: function(response) {
      if(response) {
        console.log(response)
      }
    }
  })
&lt;/LI-CODE&gt;&lt;P&gt;Testing what information it would capture in the intercept for the above bad esriRequest it looks like it gets the same "failed to fetch" error.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tentatively settled on an approach to try to load the layer and upon error run a simple javascript fetch() to the endpoint with the token we have to see what json information is returned. In this way I can detect missing/expired or invalid tokens.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I haven't tried yet but calling &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#load" target="_self"&gt;load()&lt;/A&gt; on the layer might be a way to detect errors (loadStatus) before adding to map and view.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 15 May 2021 21:19:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/esrrequest-does-not-handle-missing-token/m-p/1058301#M73121</guid>
      <dc:creator>RyanSutcliffe</dc:creator>
      <dc:date>2021-05-15T21:19:50Z</dc:date>
    </item>
  </channel>
</rss>

