agraboso / agraboso/redux-api-middleware
Proper way to handle errors / failure_actions
- 主要語言
- JavaScript
- 星號
- 1.5k
- 分支
- 190
- PR 合併指標
- 30 天內沒有已合併 PR
描述
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?
貢獻指南
評估
這個 Issue 還沒有評估資料。