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

check for errors before trying to parse response #131

Merged
merged 2 commits into from
Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions packages/arcgis-rest-request/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomwayson should this be an ArcGISRequestError? Mainly so users can avoid situations like having to check for Error vs ArcGISRequestError:

request(/* ... */).catch(error => {
  if(error instanceof Error) {
    // ok I have an error so I only have error.message...
  }

  if(error instanceof ArcGISRequestError) {
    // ok I have an ArcGISRequestError so I can access lots of other stuff...
  }
})

It might be easier if users can do this:

request(/* ... */).catch(error => {
  // error is always a ArcGISRequestError
})

This could be something like:

return Promise.reject(new ArcGISRequestError(statusText, `HTTP ${status}`));

which woudl result in error messages like HTTP 404: Not Found

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give that a go later today

}
switch (params.f) {
case "json":
return response.json();
Expand Down
14 changes: 14 additions & 0 deletions packages/arcgis-rest-request/test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
},
Expand Down Expand Up @@ -244,6 +257,7 @@ describe("request()", () => {
FormData = oldFormData;

const MockFetchResponse = {
ok: true,
json() {
return Promise.resolve(SharingRestInfo);
},
Expand Down