Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Commit

Permalink
Include status error
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien0102 committed Dec 12, 2018
1 parent a752635 commit d8676dc
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Get.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});

Expand Down Expand Up @@ -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,
});
});

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
});
});
});
Expand Down
2 changes: 2 additions & 0 deletions src/Get.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type ResolveFunction<T> = ((data: any) => T) | ((data: any) => Promise<T>
export interface GetDataError<TError> {
message: string;
data: TError | string;
status?: number;
}

/**
Expand Down Expand Up @@ -262,6 +263,7 @@ class ContextlessGet<TData, TError, TQueryParams> extends React.Component<
const error = {
message: `Failed to fetch: ${response.status} ${response.statusText}${responseError ? " - " + data : ""}`,
data,
status: response.status,
};

this.setState({
Expand Down
3 changes: 3 additions & 0 deletions src/Mutate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
});
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/Mutate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ class ContextlessMutate<TData, TError, TQueryParams, TRequestBody> 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,
Expand Down
4 changes: 4 additions & 0 deletions src/Poll.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});

Expand All @@ -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,
});
});

Expand Down Expand Up @@ -366,6 +368,7 @@ describe("Poll", () => {
expect(children.mock.calls[1][1].error).toEqual({
data: "<html>504 Gateway Time-out</html>",
message: "Failed to poll: 504 Gateway Timeout",
status: 504,
});

// second res (success)
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/Poll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ class ContextlessPoll<TData, TError, TQueryParams> 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 });

Expand Down

0 comments on commit d8676dc

Please sign in to comment.