tot*_*oob 3 javascript redux fetch-api redux-thunk react-redux
我正在使用redux thunk在操作中获取一些数据
function handleErrors(response) {
console.log(response)
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}
export const something = (var) => dispatch => {
fetch(`${url}/something`, {credentials: 'include'})
.then(handleErrors)
.then(res => res.json())
.then(res =>
dispatch({
type: SOMETHING,
payload: res
})
)
.catch(error =>
dispatch({
type: ERROR,
payload: error
})
)
Run Code Online (Sandbox Code Playgroud)
我的快递服务器上的错误响应为“某些错误”
return res.status(500).send({ message: 'some error' });
Run Code Online (Sandbox Code Playgroud)
当它获取并且是错误(500)时,其消息是通用的“内部服务器错误”。
如何获取提取中的“某些错误”?
不确定您的内容是什么handleError。提取错误消息的一种方法是这样的
fetch(url)
.then(res => {
// Check if response has errors
if (res.ok) {
// No errors
return res.json();
} else {
// Has errors, since res.json() returns a Promise, we
// chain a then here to get the value and return the err value
// as Promise rejection so that it will go to the
// catch handler
return res.json().then(err => Promise.reject(err));
// this could also be
// return res.json().then(err => throw Error(err));
}
})
.then(json => {
// dispatch success
})
.catch(err => {
// dispatch error
});
Run Code Online (Sandbox Code Playgroud)