-
Notifications
You must be signed in to change notification settings - Fork 195
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
Proper way to handle errors / failure_actions #198
Comments
Hi @donatoaz, great question!
I'm not sure what
So I'm understanding that you want 404s to be treated as FAILURE types (default behavior), but with some special logic. Is that right? If so, would something like the following work? // Part of the RSAA action
{
type: failure_action,
payload: (action, state, res) => {
return getJSON(res).then((data) => {
return {
...data,
status: res.status
}
})
}
} dispatch(fetchWorkOrder(workOrderId)) // this might respond with a 404
.then(
(action) => {
if (action.error) {
if (action.payload.status === 404) {
dispatch(push('/work-orders'))
} else {
// Other errors
}
} else {
// this should only run if there is no error
dispatch(loadServicingsForWorkOrder(workOrderId, 'unfinished'))
dispatch(fetchUnreadMessagesForWorkOrder(workOrderId))
}
}
) I do not think there is any case in which https://github.com/agraboso/redux-api-middleware#error goes into some detail about this -- please let us know if this isn't enough, or if there's something missing that you think would be helpful. |
I hope no one minds I piggyback on this, it seems sort of inline with what I'm struggling with. Also, this is the first time I've posted an issue to GitHub so feedback on how to improve communicating in this type of forum is greatly appreciated. Currently, I have a function that will send a This code doesn't call a export const submitCartOrder = (params: CartParams): Thunk => {
return async (dispatch: Dispatch) => {
const result = await dispatch(
apiAction({
endpoint: "cart/order/submit",
method: "POST",
body: JSON.stringify(params),
types: [
SubmitCartOrderActions.SUBMIT_CART_ORDER_REQUEST,
SubmitCartOrderActions.SUBMIT_CART_ORDER_SUCCESS,
SubmitCartOrderActions.SUBMIT_CART_ORDER_FAILURE,
],
}),
);
if (result.type === SubmitCartOrderActions.SUBMIT_CART_ORDER_FAILURE) {
throw new Error(result.payload);
}
return result;
};
}; However, this will once the server responds. export const submitCartOrder = (params: CartParams): Thunk => {
return async (dispatch: Dispatch) => {
const result = await dispatch(
apiAction({
endpoint: "cart/order/submit",
method: "POST",
body: JSON.stringify(params),
types: [
SubmitCartOrderActions.SUBMIT_CART_ORDER_REQUEST,
SubmitCartOrderActions.SUBMIT_CART_ORDER_SUCCESS,
{
type: SubmitCartOrderActions.SUBMIT_CART_ORDER_FAILURE,
// promise would not resolve from request when 400.
// unless using a descriptor with a payload.... no idea
payload: async (action: any, state: any, res: Response) => {
const payload = await res.json();
return payload;
},
},
],
}),
);
if (result.type === SubmitCartOrderActions.SUBMIT_CART_ORDER_FAILURE) {
throw new Error(result.payload);
}
return result;
};
}; For some context, the Some potentially important lib versions:
Does this make sense? I'd be happy to provide any more context needed. |
Hey everyone!
I started working on an app that uses
redux-api-middleware
but I am a little confused as to how error handling is/should be implemented.Currently, the failure action is as such:
In one of the containers there is a dispatch that sometimes gets a 404, and I would like it to act on that, and as such I did:
I tried the following: passing an
errorInterceptor
for the payload onfailure_action
:But that did not give me the desired results.
I ended up changing the
Promise.reject
with athrow new ApiError(response.status, response.statusText, response)
and on my container file, I changed the dispath logic above to:But it seems like the wrong thing to do, specially because there may be other errors different than 404...
What is the suggested practice in these cases?
The text was updated successfully, but these errors were encountered: