diff --git a/packages/arcgis-rest-request/src/request.ts b/packages/arcgis-rest-request/src/request.ts index 102a4aa520..10842cde13 100644 --- a/packages/arcgis-rest-request/src/request.ts +++ b/packages/arcgis-rest-request/src/request.ts @@ -193,6 +193,11 @@ export function request( return options.fetch(url, fetchOptions); }) .then(response => { + if (!response.ok) { + // server responded w/ an actual error (404, 500, etc) + const { status, statusText } = response; + return Promise.reject(new Error(`${status}: ${statusText}`)); + } switch (params.f) { case "json": return response.json(); diff --git a/packages/arcgis-rest-request/test/request.test.ts b/packages/arcgis-rest-request/test/request.test.ts index aff239244d..019fab804c 100644 --- a/packages/arcgis-rest-request/test/request.test.ts +++ b/packages/arcgis-rest-request/test/request.test.ts @@ -167,6 +167,18 @@ describe("request()", () => { }); }); + it("should re-throw HTTP errors (404, 500, etc)", done => { + fetchMock.once("*", 404); + + request( + "https://www.arcgis.com/sharing/rest/content/items/43a8e51789044d9480a20089a84129ad/data" + ).catch(error => { + expect(error.message).toBe("404: Not Found"); + expect(error instanceof Error).toBeTruthy(); + done(); + }); + }); + it("should throw errors with information about the request", done => { fetchMock.once("*", ArcGISOnlineError); @@ -189,6 +201,7 @@ describe("request()", () => { it("should allow you to use custom implementations of `fetch`", done => { const MockFetchResponse = { + ok: true, json() { return Promise.resolve(SharingRestInfo); }, @@ -244,6 +257,7 @@ describe("request()", () => { FormData = oldFormData; const MockFetchResponse = { + ok: true, json() { return Promise.resolve(SharingRestInfo); },