-
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
Custom payload function is overwritten in RequestError & InternalError instances #257
Comments
I think this is intended by design. This happens when your fetch is something custom and throws an explicit error. redux-api-middleware/src/middleware.js Lines 209 to 222 in 2263853
|
Well, it does make little sense in network failure (lost internet connection or any other exception fetch throws) to dispatch the same action type of failure, but do not respect payload function in these cases. Feels like a mistake, as the API basically is inconsistent and is prone to unhandled cases. This is exactly how i discovered this 😄 |
Hmm, maybe I don't understand exactly your use case. Any network failure or lost connection, you don't have any payload. async function doFetch(input: RequestInfo, init?: RequestInit): Promise<Response> {
const _res = await fetch(input, init)
const contentType = _res.headers.get('Content-Type')
if (_res.ok && contentType && contentType.indexOf('json') !== -1) {
const res = _res.clone()
const json = await _res.json()
if (json.status === 'ERROR') {
// all `Response` properties are readonly,
// so we need `Object.defineProperty` to override them
Object.defineProperty(res, 'statusText', { value: 'Internal Server Error' })
Object.defineProperty(res, 'status', { value: 500 })
Object.defineProperty(res, 'ok', { value: false })
}
return res
}
return _res
} |
Yes, you are correct, but how could i handle what is inserted in the store in such cases? I could, of course do some hacking in the fetch implementation, as you given in the example above, but i don't find this the best solution in such a situation. Even in a network error case, the payload error is inserted in the store. I create a custom shape for the data, so this basically breaks, as i have no possibility to edit the data that is stored, unless i wrote a middleware (overkill) or something. |
I have a slightly different use case for this. I need my export const login = ({ login: username, password }) =>
createAction({
endpoint: `${apiBase}/v2/oauth/token`,
method,
headers,
bailout: () => {
if (FORBIDDEN_USERNAMES.includes(username.toLowerCase())) {
console.debug('username forbidden', username)
throw new Error('Disallowed Username')
}
return true
},
body: `username=${username}&password=${password}&grant_type=password`,
types: ['LOGIN', 'LOGIN_SUCCESS', 'LOGIN_FAIL']
}) It would be ideal to be able to report this error rather than simply logging it. Alas when the bailout throws it simply gives me the FYI I worked around the issue of errors not triggering
export const failOnError = ({ dispatch }) => next => async action => {
const { type, error } = action
if (!error || !type.endsWith('_SUCCESS')) return next(action)
const failType = type.replace('_SUCCESS', '_FAIL')
dispatch({
...action,
type: failType
})
} |
Not sure if this is intended by design or not, but currently, it's not possible to handle these types of errors in the
FETCH_ERROR
, payload function.It gets overwritten, here for example
redux-api-middleware/src/middleware.js
Line 185 in 2263853
Don't think this should be the case
The text was updated successfully, but these errors were encountered: