【发布时间】:2018-12-30 00:55:13
【问题描述】:
这对我来说是新的,我试图弄清楚当 axios 使用 yield generator (redux-saga) 发生错误时如何检索请求正文
这是我正在使用的一段代码:
function requestLogin(user: ILoginUserPayload) {
const loginUrl = `${config.apiUrl}/auth/logIn`;
return axios.post(loginUrl, user);
}
export default function* watchAuthAction(): IterableIterator<any> {
while (true) {
const { payload }: { payload: ILoginUserPayload} = yield take(TYPES.LOGIN_REQUEST);
console.log(`Payload`, payload);
try {
const response = yield requestLogin(payload);
console.log(`Response`, response);
} catch (error) {
// According to my debug, error.request contains all the axios info
console.log(error.request.response);
yield put({ type: TYPES.LOGIN_FAILURE, error: error.request.response.message });
}
}
}
我用VSCode调试了错误内容,发现里面有axios的请求信息(我的意思是,我99%肯定axios会报错,就到这里了)
我的身体反应是这样的:
{"message":"User doesn't exist","stack":"Error: User doesn't exist"}
所以我试着用:
console.log(error.request.response.message);
但是不行,可能是我忘记了关于axios的一些事情……
有什么想法吗?
编辑:我实际上正在阅读这部分:https://github.com/axios/axios#handling-errors 但我仍然无法访问我的数据:/
【问题讨论】:
标签: reactjs error-handling axios redux-saga