Axios 可能正在解析响应。我在我的代码中访问这样的错误:
axios({
method: 'post',
responseType: 'json',
url: `${SERVER_URL}/token`,
data: {
idToken,
userEmail
}
})
.then(response => {
dispatch(something(response));
})
.catch(error => {
dispatch({ type: AUTH_FAILED });
dispatch({ type: ERROR, payload: error.data.error.message });
});
从文档:
请求的响应包含以下信息。
{
// `data` is the response that was provided by the server
data: {},
// `status` is the HTTP status code from the server response
status: 200,
// `statusText` is the HTTP status message from the server response
statusText: 'OK',
// `headers` the headers that the server responded with
headers: {},
// `config` is the config that was provided to `axios` for the request
config: {}
}
所以catch(error => )
实际上只是catch(response => )
编辑:
我仍然不明白为什么记录错误会返回该堆栈消息。我试着像这样记录它。然后你实际上可以看到它是一个对象。
console.log('errorType', typeof error);
console.log('error', Object.assign({}, error));
编辑2:
环顾四周后,这就是您要打印的内容。这是一个 Javascipt 错误对象。爱可信则增强了这种错误与配置,代码和效应初探喜欢这样。
console.log('error', error);
console.log('errorType', typeof error);
console.log('error', Object.assign({}, error));
console.log('getOwnPropertyNames', Object.getOwnPropertyNames(error));
console.log('stackProperty', Object.getOwnPropertyDescriptor(error, 'stack'));
console.log('messageProperty', Object.getOwnPropertyDescriptor(error, 'message'));
console.log('stackEnumerable', error.propertyIsEnumerable('stack'));
console.log('messageEnumerable', error.propertyIsEnumerable('message'));