This example demonstrates how to handle all four error types
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { request, ErrorTypes } from"@esri/arcgis-rest-request";
request(url, {
authentication: authenticationManager
})
.then(response => {
console.log(response);
}).catch(e => {
switch(e.name) {
case ErrorTypes.ArcGISRequestError:
// handle a general error from the APIbreak;
case ErrorTypes.ArcGISAuthError:
// handle an authentication errorbreak;
case ErrorTypes.ArcGISAccessDeniedError:
// handle a user denying an authorization request in an oAuth workflowbreak;
case ErrorTypes.ArcGISTokenRequestError:
// handle an error response trying to generate a new access tokenbreak;
default:
// handle some other error (usually a network error) }
});
Request errors
The base error type in ArcGIS REST JS is ArcGISRequestError, which extends the native Error object. ArcGISRequestError add several additional properties that contain the full response from the server, the original request URL, and the options of the request.
If ArcGIS REST JS encounters an "invalid token" response from a service, an ArcGISAuthError will be thrown. ArcGISAuthError has an additional retry() method that allows you to pass in a new authentication manager to attempt the request again.
If the authentication manager cannot refresh the token successfully a generate token error will be thrown instead.
1
2
3
4
5
6
7
8
request(someUrl, {
authentication: someAuthenticationManager
}).catch(e => {
if(e.name === "ArcGISTokenRequestError") {
// ArcGIS REST JS could not generate an appropriate token for this request.// All credentials are likely invalid and the authentication process should be restarted. }
})
Unable to federate with ArcGIS Server
ArcGISTokenRequestError will also be thrown if you are using an ArcGIS Server portal and make a request to a service that is not federated with that portal.
The credentials in the authentication manager used for the failed request are likely still valid, but they cannot be used to make the request.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { request, ArcGISIdentityManager } from"@esri/arcgis-rest-request";
import { getService } from'@esri/arcgis-rest-feature-service';
ArcGISIdentityManager.beginOAuth2({
clientId: "•••"redurectUri: "•••",
portal: "https://my-organziation.com/my-arcgis-portal/"}).then(manager => {
// get information about a private service getService("https://other-organization/arcgis/rest/services/ExampleService/FeatureServer/", {
authentication: manager
})
.then((info)=>{
// If the server is federated with the portal the authentication// will be used and the request will be successful })
.catch((e)=>{
// otherwise we can check to see if this is a not federated error.// potentially this could be used to prompt the user to sign into// the appropriate portal.if(e.name === "ArcGISAuthError" && e.code === "NOT_FEDERATED") {
}
})
})