Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: catch err internally when getItemData doesnt find any #505

Merged
merged 6 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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