Skip to content

Commit

Permalink
just let request throw its own error
Browse files Browse the repository at this point in the history
  • Loading branch information
jgravois committed Sep 13, 2018
1 parent 49f0902 commit 31e56db
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 173 deletions.
26 changes: 3 additions & 23 deletions packages/arcgis-rest-feature-service-admin/src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,12 @@ export interface IAddToServiceDefinitionItemSummary {
id: any;
}

export interface IAddToServiceDefinitionSuccessResult {
export interface IAddToServiceDefinitionResult {
layers?: IAddToServiceDefinitionItemSummary[];
tables?: IAddToServiceDefinitionItemSummary[];
success: boolean;
}

export interface IAddToServiceDefinitionFailureResult {
error: {
code: number;
message: string;
details: string[];
};
}

/**
* Add layer(s) and/or table(s) to a hosted feature service.
*
Expand All @@ -57,9 +49,7 @@ export interface IAddToServiceDefinitionFailureResult {
export function addToServiceDefinition(
url: string,
requestOptions: IAddToServiceDefinitionRequestOptions
): Promise<
IAddToServiceDefinitionSuccessResult | IAddToServiceDefinitionFailureResult
> {
): Promise<IAddToServiceDefinitionResult> {
const adminUrl =
url.replace("/rest/services", "/rest/admin/services") + "/addToDefinition";

Expand All @@ -76,15 +66,5 @@ export function addToServiceDefinition(
requestOptions.params.addToDefinition.tables = requestOptions.tables;
}

return new Promise((resolve, reject) => {
request(adminUrl, requestOptions).then(
response => {
resolve(response);
},
response => {
// We're not interested in the full ArcGISRequestError response
reject(response.response);
}
);
});
return request(adminUrl, requestOptions);
}
14 changes: 6 additions & 8 deletions packages/arcgis-rest-feature-service-admin/test/mocks/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
* Apache-2.0 */

import { ICreateServiceResult } from "../../src/create";
import {
IAddToServiceDefinitionSuccessResult,
IAddToServiceDefinitionFailureResult
} from "../../src/update";
import { IAddToServiceDefinitionResult } from "../../src/update";

export const FeatureServiceSuccessResponse: ICreateServiceResult = {
encodedServiceURL:
Expand All @@ -24,7 +21,7 @@ export const FeatureServiceFailResponse: any = {
success: false
};

export const AddToFeatureServiceSuccessResponseFredAndGinger: IAddToServiceDefinitionSuccessResult = {
export const AddToFeatureServiceSuccessResponseFredAndGinger: IAddToServiceDefinitionResult = {
layers: [
{
name: "Fred",
Expand All @@ -37,7 +34,7 @@ export const AddToFeatureServiceSuccessResponseFredAndGinger: IAddToServiceDefin
],
success: true
};
export const AddToFeatureServiceSuccessResponseFayardAndHarold: IAddToServiceDefinitionSuccessResult = {
export const AddToFeatureServiceSuccessResponseFayardAndHarold: IAddToServiceDefinitionResult = {
tables: [
{
name: "Fayard",
Expand All @@ -50,7 +47,7 @@ export const AddToFeatureServiceSuccessResponseFayardAndHarold: IAddToServiceDef
],
success: true
};
export const AddToFeatureServiceSuccessResponseCydAndGene: IAddToServiceDefinitionSuccessResult = {
export const AddToFeatureServiceSuccessResponseCydAndGene: IAddToServiceDefinitionResult = {
layers: [
{
name: "Cyd",
Expand All @@ -65,7 +62,8 @@ export const AddToFeatureServiceSuccessResponseCydAndGene: IAddToServiceDefiniti
],
success: true
};
export const AddToFeatureServiceFailResponse: IAddToServiceDefinitionFailureResult = {

export const AddToFeatureServiceError: any = {
error: {
code: 400,
message: "Unable to add feature service definition.",
Expand Down
220 changes: 78 additions & 142 deletions packages/arcgis-rest-feature-service-admin/test/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {
AddToFeatureServiceSuccessResponseFredAndGinger,
AddToFeatureServiceSuccessResponseFayardAndHarold,
AddToFeatureServiceSuccessResponseCydAndGene,
AddToFeatureServiceFailResponse
AddToFeatureServiceError
} from "./mocks/service";

import { UserSession } from "@esri/arcgis-rest-auth";
import { TOMORROW } from "@esri/arcgis-rest-auth/test/utils";
import { encodeParam } from "@esri/arcgis-rest-request";
import { encodeParam, ErrorTypes } from "@esri/arcgis-rest-request";
import { ILayer, ITable } from "@esri/arcgis-rest-common-types";

describe("add to feature service", () => {
Expand Down Expand Up @@ -192,138 +192,94 @@ describe("add to feature service", () => {
...MOCK_USER_REQOPTS
}
)
.then(
response => {
// Check service call
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall(
"*"
);

expect(url).toEqual(
"https://services1.arcgis.com/ORG/arcgis/rest/admin/services/FEATURE_SERVICE/FeatureServer/addToDefinition"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
expect(options.body).toContain(encodeParam("token", "fake-token"));
expect(options.body).toContain(
encodeParam(
"addToDefinition",
JSON.stringify({
layers: [layerDescriptionCyd],
tables: [tableDescriptionGene]
})
)
);

// Check response
expect(response).toEqual(
AddToFeatureServiceSuccessResponseCydAndGene
);

done();
},
() => {
fail(); // call is supposed to succeed
}
)
.then(response => {
// Check service call
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");

expect(url).toEqual(
"https://services1.arcgis.com/ORG/arcgis/rest/admin/services/FEATURE_SERVICE/FeatureServer/addToDefinition"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
expect(options.body).toContain(encodeParam("token", "fake-token"));
expect(options.body).toContain(
encodeParam(
"addToDefinition",
JSON.stringify({
layers: [layerDescriptionCyd],
tables: [tableDescriptionGene]
})
)
);

// Check response
expect(response).toEqual(
AddToFeatureServiceSuccessResponseCydAndGene
);
done();
})
.catch(e => {
fail(e);
});
});

it("should fail to add a bad layer", done => {
fetchMock.once("*", AddToFeatureServiceFailResponse);
fetchMock.once("*", AddToFeatureServiceError);

addToServiceDefinition(
"https://services1.arcgis.com/ORG/arcgis/rest/services/FEATURE_SERVICE/FeatureServer",
{
layers: [layerDescriptionFail],
...MOCK_USER_REQOPTS
}
)
.then(
e => {
fail(e); // call is supposed to fail
},
response => {
// Check service call
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall(
"*"
);

expect(url).toEqual(
"https://services1.arcgis.com/ORG/arcgis/rest/admin/services/FEATURE_SERVICE/FeatureServer/addToDefinition"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
expect(options.body).toContain(encodeParam("token", "fake-token"));
expect(options.body).toContain(
encodeParam(
"addToDefinition",
JSON.stringify({ layers: [layerDescriptionFail] })
)
);

// Check response
expect(response).toEqual(AddToFeatureServiceFailResponse);

done();
}
)
.catch(e => {
fail(e);
).catch(error => {
expect(error.name).toBe(ErrorTypes.ArcGISRequestError);
expect(error.message).toBe(
"400: Unable to add feature service definition."
);
expect(error instanceof Error).toBeTruthy();
expect(error.url).toBe(
"https://services1.arcgis.com/ORG/arcgis/rest/admin/services/FEATURE_SERVICE/FeatureServer/addToDefinition"
);
// params added internally aren't surfaced in the error
expect(error.options.params.addToDefinition).toEqual({
layers: [layerDescriptionFail]
});
expect(error.options.httpMethod).toEqual("POST");
done();
});
});

it("should fail to add a bad table", done => {
fetchMock.once("*", AddToFeatureServiceFailResponse);
fetchMock.once("*", AddToFeatureServiceError);

addToServiceDefinition(
"https://services1.arcgis.com/ORG/arcgis/rest/services/FEATURE_SERVICE/FeatureServer",
{
tables: [tableDescriptionFail],
...MOCK_USER_REQOPTS
}
)
.then(
e => {
fail(e); // call is supposed to fail
},
response => {
// Check service call
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall(
"*"
);

expect(url).toEqual(
"https://services1.arcgis.com/ORG/arcgis/rest/admin/services/FEATURE_SERVICE/FeatureServer/addToDefinition"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
expect(options.body).toContain(encodeParam("token", "fake-token"));
expect(options.body).toContain(
encodeParam(
"addToDefinition",
JSON.stringify({ tables: [tableDescriptionFail] })
)
);

// Check response
expect(response).toEqual(AddToFeatureServiceFailResponse);

done();
}
)
.catch(e => {
fail(e);
).catch(error => {
expect(error.name).toBe(ErrorTypes.ArcGISRequestError);
expect(error.message).toBe(
"400: Unable to add feature service definition."
);
expect(error instanceof Error).toBeTruthy();
expect(error.url).toBe(
"https://services1.arcgis.com/ORG/arcgis/rest/admin/services/FEATURE_SERVICE/FeatureServer/addToDefinition"
);
// params added internally aren't surfaced in the error
expect(error.options.params.addToDefinition).toEqual({
tables: [tableDescriptionFail]
});
expect(error.options.httpMethod).toEqual("POST");
done();
});
});

it("should fail to add a bad layer and a bad table", done => {
fetchMock.once("*", AddToFeatureServiceFailResponse);
fetchMock.once("*", AddToFeatureServiceError);

addToServiceDefinition(
"https://services1.arcgis.com/ORG/arcgis/rest/services/FEATURE_SERVICE/FeatureServer",
Expand All @@ -332,43 +288,23 @@ describe("add to feature service", () => {
tables: [tableDescriptionFail],
...MOCK_USER_REQOPTS
}
)
.then(
e => {
fail(e); // call is supposed to fail
},
response => {
// Check service call
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall(
"*"
);

expect(url).toEqual(
"https://services1.arcgis.com/ORG/arcgis/rest/admin/services/FEATURE_SERVICE/FeatureServer/addToDefinition"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
expect(options.body).toContain(encodeParam("token", "fake-token"));
expect(options.body).toContain(
encodeParam(
"addToDefinition",
JSON.stringify({
layers: [layerDescriptionFail],
tables: [tableDescriptionFail]
})
)
);

// Check response
expect(response).toEqual(AddToFeatureServiceFailResponse);

done();
}
)
.catch(e => {
fail(e);
).catch(error => {
expect(error.name).toBe(ErrorTypes.ArcGISRequestError);
expect(error.message).toBe(
"400: Unable to add feature service definition."
);
expect(error instanceof Error).toBeTruthy();
expect(error.url).toBe(
"https://services1.arcgis.com/ORG/arcgis/rest/admin/services/FEATURE_SERVICE/FeatureServer/addToDefinition"
);
// params added internally aren't surfaced in the error
expect(error.options.params.addToDefinition).toEqual({
tables: [tableDescriptionFail],
layers: [layerDescriptionFail]
});
expect(error.options.httpMethod).toEqual("POST");
done();
});
});
}); // auth requests
});

0 comments on commit 31e56db

Please sign in to comment.