diff --git a/src/Get.test.tsx b/src/Get.test.tsx index b904bea7..1077e97f 100644 --- a/src/Get.test.tsx +++ b/src/Get.test.tsx @@ -152,6 +152,7 @@ describe("Get", () => { expect(children.mock.calls[1][1].error).toEqual({ data: { message: "You shall not pass!" }, message: "Failed to fetch: 401 Unauthorized", + status: 401, }); }); @@ -199,6 +200,7 @@ describe("Get", () => { "invalid json response body at https://my-awesome-api.fake reason: Unexpected token < in JSON at position 0", message: "Failed to fetch: 200 OK - invalid json response body at https://my-awesome-api.fake reason: Unexpected token < in JSON at position 0", + status: 200, }); }); @@ -223,6 +225,7 @@ describe("Get", () => { { data: { message: "You shall not pass!" }, message: "Failed to fetch: 401 Unauthorized", + status: 401, }, expect.any(Function), // retry expect.any(Object), // response @@ -253,6 +256,7 @@ describe("Get", () => { { data: { message: "You shall not pass!" }, message: "Failed to fetch: 401 Unauthorized", + status: 401, }, expect.any(Function), // retry expect.any(Object), // response @@ -471,6 +475,7 @@ describe("Get", () => { expect(children.mock.calls[0][1].error).toEqual({ data: "Go away!", message: "Failed to fetch: 401 Unauthorized", + status: 401, }); }); }); diff --git a/src/Get.tsx b/src/Get.tsx index 5cad4540..d3d0d23c 100644 --- a/src/Get.tsx +++ b/src/Get.tsx @@ -16,6 +16,7 @@ export type ResolveFunction = ((data: any) => T) | ((data: any) => Promise export interface GetDataError { message: string; data: TError | string; + status?: number; } /** @@ -262,6 +263,7 @@ class ContextlessGet extends React.Component< const error = { message: `Failed to fetch: ${response.status} ${response.statusText}${responseError ? " - " + data : ""}`, data, + status: response.status, }; this.setState({ diff --git a/src/Mutate.test.tsx b/src/Mutate.test.tsx index 8a838fd6..a28b6382 100644 --- a/src/Mutate.test.tsx +++ b/src/Mutate.test.tsx @@ -293,6 +293,7 @@ describe("Mutate", () => { expect(error).toEqual({ data: { error: "oh no… not again…" }, message: "Failed to fetch: 500 Internal Server Error", + status: 500, }); }); }); @@ -324,6 +325,7 @@ describe("Mutate", () => { { data: { message: "You shall not pass!" }, message: "Failed to fetch: 401 Unauthorized", + status: 401, }, expect.any(Function), // retry expect.any(Object), // response @@ -360,6 +362,7 @@ describe("Mutate", () => { { data: { message: "You shall not pass!" }, message: "Failed to fetch: 401 Unauthorized", + status: 401, }, expect.any(Function), // retry expect.any(Object), // response diff --git a/src/Mutate.tsx b/src/Mutate.tsx index e782303c..826c78c3 100644 --- a/src/Mutate.tsx +++ b/src/Mutate.tsx @@ -171,7 +171,11 @@ class ContextlessMutate extends React return; } if (!response.ok || responseError) { - const error = { data, message: `Failed to fetch: ${response.status} ${response.statusText}` }; + const error = { + data, + message: `Failed to fetch: ${response.status} ${response.statusText}`, + status: response.status, + }; this.setState({ loading: false, diff --git a/src/Poll.test.tsx b/src/Poll.test.tsx index 357096f0..22d49bd7 100644 --- a/src/Poll.test.tsx +++ b/src/Poll.test.tsx @@ -304,6 +304,7 @@ describe("Poll", () => { expect(children.mock.calls[1][1].error).toEqual({ data: { message: "You shall not pass!" }, message: "Failed to poll: 401 Unauthorized", + status: 401, }); }); @@ -330,6 +331,7 @@ describe("Poll", () => { "invalid json response body at https://my-awesome-api.fake reason: Unexpected token < in JSON at position 0", message: "Failed to poll: 200 OK - invalid json response body at https://my-awesome-api.fake reason: Unexpected token < in JSON at position 0", + status: 200, }); }); @@ -366,6 +368,7 @@ describe("Poll", () => { expect(children.mock.calls[1][1].error).toEqual({ data: "504 Gateway Time-out", message: "Failed to poll: 504 Gateway Timeout", + status: 504, }); // second res (success) @@ -394,6 +397,7 @@ describe("Poll", () => { { data: { message: "You shall not pass!" }, message: "Failed to poll: 401 Unauthorized", + status: 401, }, expect.any(Function), // retry expect.any(Object), // response diff --git a/src/Poll.tsx b/src/Poll.tsx index 5c8a5f0b..6bec59a5 100644 --- a/src/Poll.tsx +++ b/src/Poll.tsx @@ -246,6 +246,7 @@ class ContextlessPoll extends React.Component< const error = { message: `Failed to poll: ${response.status} ${response.statusText}${responseError ? " - " + data : ""}`, data, + status: response.status, }; this.setState({ loading: false, lastResponse: response, error });