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

Commit

Permalink
Add Response in the onError callback
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien0102 committed Nov 13, 2018
1 parent 2d1891c commit 04fcd52
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface RestfulReactProviderProps<T = any> {
* Depending of your case, it can be easier to add a `localErrorOnly` on your `Mutate` component
* to deal with your retry locally instead of in the provider scope.
*/
onError?: (err: any, retry?: () => Promise<T | null>) => void;
onError?: (err: any, retry: () => Promise<T | null>, response: Response) => void;
}

const { Provider, Consumer: RestfulReactConsumer } = React.createContext<Required<RestfulReactProviderProps>>({
Expand Down
6 changes: 4 additions & 2 deletions src/Get.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ describe("Get", () => {
data: { message: "You shall not pass!" },
message: "Failed to fetch: 401 Unauthorized",
},
expect.any(Function),
expect.any(Function), // retry
expect.any(Object), // response
);
});

Expand Down Expand Up @@ -232,7 +233,8 @@ describe("Get", () => {
data: { message: "You shall not pass!" },
message: "Failed to fetch: 401 Unauthorized",
},
expect.any(Function),
expect.any(Function), // retry
expect.any(Object), // response
);
onError.mock.calls[0][1]();
await wait(() => expect(children.mock.calls.length).toBe(4));
Expand Down
2 changes: 1 addition & 1 deletion src/Get.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class ContextlessGet<TData, TError> extends React.Component<
});

if (!this.props.localErrorOnly && this.props.onError) {
this.props.onError(error, () => this.fetch(requestPath, thisRequestOptions));
this.props.onError(error, () => this.fetch(requestPath, thisRequestOptions), response);
}

return null;
Expand Down
6 changes: 4 additions & 2 deletions src/Mutate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ describe("Mutate", () => {
data: { message: "You shall not pass!" },
message: "Failed to fetch: 401 Unauthorized",
},
expect.any(Function),
expect.any(Function), // retry
expect.any(Object), // response
);
});

Expand Down Expand Up @@ -325,7 +326,8 @@ describe("Mutate", () => {
data: { message: "You shall not pass!" },
message: "Failed to fetch: 401 Unauthorized",
},
expect.any(Function),
expect.any(Function), // retry
expect.any(Object), // response
);
const data = await onError.mock.calls[0][1]();
expect(data).toEqual({ message: "You shall pass :)" });
Expand Down
2 changes: 1 addition & 1 deletion src/Mutate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class ContextlessMutate<TData, TError> extends React.Component<
});

if (!this.props.localErrorOnly && this.props.onError) {
this.props.onError(error, () => this.mutate(body, mutateRequestOptions));
this.props.onError(error, () => this.mutate(body, mutateRequestOptions), response);
}

throw error;
Expand Down
12 changes: 8 additions & 4 deletions src/Poll.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,14 @@ describe("Poll", () => {
);

await wait(() => expect(children.mock.calls.length).toBe(2));
expect(onError).toBeCalledWith({
data: { message: "You shall not pass!" },
message: "Failed to poll: 401 Unauthorized",
});
expect(onError).toBeCalledWith(
{
data: { message: "You shall not pass!" },
message: "Failed to poll: 401 Unauthorized",
},
expect.any(Function), // retry
expect.any(Object), // response
);
});

it("should set the `error` object properly", async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/Poll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ class ContextlessPoll<TData, TError> extends React.Component<
this.setState({ loading: false, lastResponse: response, error });

if (!this.props.localErrorOnly && this.props.onError) {
this.props.onError(error);
this.props.onError(error, () => Promise.resolve(), response);
}
} else if (this.isModified(response, data)) {
this.setState(prevState => ({
Expand Down

0 comments on commit 04fcd52

Please sign in to comment.