Skip to content

Commit

Permalink
fix: catch err internally when getItemData doesnt find any (#505)
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/arcgis-rest-items

ISSUES CLOSED: #504
  • Loading branch information
john gravois authored Apr 9, 2019
1 parent 1bd59ee commit 914a5be
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
14 changes: 12 additions & 2 deletions packages/arcgis-rest-items/src/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function getItem(
* getItemData("ae7", { authentication })
* .then(response)
* ```
* Get the /data for an item. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/item-data.htm) for more information.
* Get the /data for an item. If no data exists, returns `undefined`. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/item-data.htm) for more information.
* @param id - Item Id
* @param requestOptions - Options for the request
* @returns A Promise that will resolve with the json data for the item.
Expand All @@ -75,7 +75,17 @@ export function getItemData(
options.params.f = null;
}

return request(url, options);
return request(url, options).catch(err => {
/* if the item doesn't include data, the response will be empty
and the internal call to response.json() will fail */
const emptyResponseErr = RegExp(
/Unexpected end of (JSON input|data at line 1 column 1)/i
);
/* istanbul ignore else */
if (emptyResponseErr.test(err.message)) {
return;
} else throw err;
});
}

export interface IGetRelatedItemsResponse {
Expand Down
25 changes: 24 additions & 1 deletion packages/arcgis-rest-items/test/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe("get", () => {
fetchMock.once("*", ItemResponse);

getItem("3ef")
.then(response => {
.then(() => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
Expand Down Expand Up @@ -90,6 +90,29 @@ describe("get", () => {
}
});

it("should return a valid response even when no data is retrieved", done => {
fetchMock.once("*", {
sendAsJson: false,
headers: { "Content-Type": "text/plain;charset=utf-8" },
body: ""
});

getItemData("3ef")
.then(response => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://www.arcgis.com/sharing/rest/content/items/3ef/data?f=json"
);
expect(options.method).toBe("GET");
expect(response).toBe(undefined);
done();
})
.catch(e => {
fail(e);
});
});

it("should return related items", done => {
fetchMock.once("*", RelatedItemsResponse);

Expand Down

0 comments on commit 914a5be

Please sign in to comment.