agraboso / agraboso/redux-api-middleware
Proper way to handle errors / failure_actions
- Ngôn ngữ chính
- JavaScript
- Star
- 1.5k
- Fork
- 190
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
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:
```javascript
...
{
type: failure_action,
payload: (action, state, res) => {
if (errorInterceptor) return getJSON(res).then((data) => errorInterceptor(data, res))
return getJSON(res).then((data) => {
return {
...data,
status: res.status
}
})
},
meta: (action, state, res) => {
return {
status: res.status,
statusText: res.statusText
}
}
}
...
```
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:
```javascript
dispatch(fetchWorkOrder(workOrderId)) // this might respond with a 404
.then(
() => { // this should only run if there is no error
dispatch(loadServicingsForWorkOrder(workOrderId, 'unfinished'))
dispatch(fetchUnreadMessagesForWorkOrder(workOrderId))
},
(e) => {
console.log('This is not running...', e)
}
)
.catch((e) => console.log('And neither is this...', e))
```
I tried the following: passing an `errorInterceptor` for the payload on `failure_action`:
```javascript
errorInterceptor: (data, response) => {
console.log('getting error here')
Promise.reject(new ApiError('testing reject'))
}
```
But that did not give me the desired results.
I ended up changing the `Promise.reject` with a `throw new ApiError(response.status, response.statusText, response)` and on my container file, I changed the dispath logic above to:
```javascript
dispatch(fetchWorkOrder(workOrderId)) // this might respond with a 404
.then(
({meta : {status = null} = {}}) => {
if (status === 404) {
dispatch(push('/work-orders'))
} else {
dispatch(loadServicingsForWorkOrder(workOrderId, 'unfinished'))
dispatch(fetchUnreadMessagesForWorkOrder(workOrderId))
}
})
```
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?
Hướng dẫn đóng góp
Đánh giá
Issue này chưa được đánh giá.