【发布时间】:2023-03-17 14:18:01
【问题描述】:
我正在使用这个反应 redux starter,我想知道如何将所有 api 500 错误重定向到一个页面?
【问题讨论】:
-
你能捕捉到前端的错误吗?如果可以,请使用您想要直接访问的页面推送到历史记录。你可以在 react-router 的文档中查看实现细节。
标签: reactjs express react-redux http-status-code-500
我正在使用这个反应 redux starter,我想知道如何将所有 api 500 错误重定向到一个页面?
【问题讨论】:
标签: reactjs express react-redux http-status-code-500
redux-starter 默认安装了react-router-redux 和redux-thunk。
我不确定 API 调用是在哪里进行的,因为您没有提供任何代码,但如果您可以将商店注入您的 API 包装器,也许您可以通过您的 redux 商店调度路由器操作。
假设您正在使用某种 API 包装器和 thunk,您可以执行以下操作:
import { push } from 'react-router-redux';
function myThunkFunction() {
return dispatch => axios.get(...).catch((error) => {
/** insert logic to determine 500 error **/
if (500error) {
dispatch(push('http://example.com/error/500'));
}
});
}
【讨论】: