reactjs - 抛出错误时,如何读取错误数据
问题描述
我正在尝试读取/处理 React JS 项目中的错误案例,但无法解析/读取 catch 中的错误数据。无法从错误数据中获取“代码”字段。
try {
dispatch({ type: types.READ_DATA })
const response = await new DataApi().readData(action.payload);
processResponse(response, [200], "Read data failed", dispatch);
dispatch({
type: types.DATA_READ,
data: response.data
})
} catch (error) {
console.error("MIDDLEWARE. CATCH ERROR: ", error);
dispatch({ type: types.DATA_FAILED, data: { Code: error.code } })
}
const processResponse = (response, okCodes, errorMessage, dispatch) => {
if (response && response.status) {
if (okCodes.indexOf(response.status) < 0) {
errorHandler(response.status, dispatch);
throw Error({ code: response.status, message: errorMessage });
}
} else {
throw Error("Error reading response status");
}
}
解决方案
用于console.error(JSON.stringify(error))
获取字符串格式的对象。
推荐阅读
- python - Plotly:在 3D Cone 中设置颜色
- macos - 无法从我的 Mac 上的 Genero Studio 连接到 Virtual Box Linux 环境中的 Informix 数据库服务器设置
- android - Android Navigation java.language.IllegalStateException 片段类未设置
- c# - 我使用“HttpContext.Current”,但调试时出现错误“HttpContext”不包含“Current”的定义
- firebase - Firebase init - 错误:意外的规则格式.rules
- ssh - 浏览器可以通过 ssh(ssh 隧道)进行本地端口隧道吗?
- google-apps-script - 希望创建一个可以提出问题的 Google 表单
- php - 从 Drupal 的后台执行一个 php 脚本
- react-native - 目前关于将数据传递到标签导航以进行反应导航的共识是什么?
- c# - 从模板(.oft 文件)发送电子邮件 - 使用 C# 的 Exchange 服务