From 914a5be114362e1c124d0003d448cbb5b35aeeb2 Mon Sep 17 00:00:00 2001 From: john gravois Date: Tue, 9 Apr 2019 13:27:40 -0700 Subject: [PATCH] fix: catch err internally when getItemData doesnt find any (#505) AFFECTS PACKAGES: @esri/arcgis-rest-items ISSUES CLOSED: #504 --- packages/arcgis-rest-items/src/get.ts | 14 ++++++++++-- packages/arcgis-rest-items/test/get.test.ts | 25 ++++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/packages/arcgis-rest-items/src/get.ts b/packages/arcgis-rest-items/src/get.ts index 634c2c49fd..18c921bbc6 100644 --- a/packages/arcgis-rest-items/src/get.ts +++ b/packages/arcgis-rest-items/src/get.ts @@ -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. @@ -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 { diff --git a/packages/arcgis-rest-items/test/get.test.ts b/packages/arcgis-rest-items/test/get.test.ts index 69701970a8..29dc07cc27 100644 --- a/packages/arcgis-rest-items/test/get.test.ts +++ b/packages/arcgis-rest-items/test/get.test.ts @@ -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( @@ -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);