javascript - 全局错误处理程序 react/redux saga 并重定向外部组件
问题描述
我正在开发一个使用redux-saga
.
我对此有点坚持,所以我正在寻求一些帮助/指导。
我正在尝试实现一个全局错误处理程序,目标是每次 api 发回500
用户时都应该重定向到 500 页。
我认为这样做的一个好方法是使用axios interceptors
这是我的拦截器(略读到最低限度):
import axios from 'axios';
import history from '../components/history'; // this is trouble
export default {
configureAxios: (store) => {
// Add a response interceptor
axios.interceptors.response.use((response) => {
// will work on this later
return response;
}, (error) => {
// catches if the session ended!
return Promise.reject(error);
});
axios.interceptors.response.use(undefined, (error) => {
// still very much work in progress
if (error.response.status >= 500) {
// maybe fire off an action to update the store
history.push('/500');
}
return Promise.reject(error);
});
}
};
这是一个好方法吗?
如果是,我有一个问题,在哪里history.push
更新了 url 但没有重定向到页面。我查看了这个答案,但无法正常工作。由于它使用的是react-router-dom
v4,我虽然遵循这里的建议
npm i history --save
我创建了我的安装历史history.js
import { createBrowserHistory } from 'history';
export default createBrowserHistory();
然后在root.js
import history from './history';
//some stuff here
const App = ({ matchedRoutes }) =>
<div>
<Head />
<Header />
<Switch>
{matchedRoutes.map((route, i) =>
<RouteWithSubRoutes key={i} {...route} />
)}
</Switch>
<Main />
</div>;
const Root = ({ store }) => (
<Provider store={store}>
<Router history={history}>
<App matchedRoutes={routes} />
</Router>
</Provider>
);
知道我做错了什么吗?关于如何更好地实施这一点的任何建议?
解决方案
推荐阅读
- python - 在 matplotlib 中绘制二叉树
- python - 无法打开文件映射对象错误?
- python - PySpark 中是否有与 Pandas 聚合函数 any() 等效的函数?
- java - 如何在 JUnit 测试中抛出 UnsupportedEncodingException?
- sql - 如何在字段(SQL)中返回一组唯一的字符?
- php - 无法接收任何针对 AWS SNS 服务的发布数据或订阅 URL
- java - 如何使 ConcurrentHashMap.entrySet().removeIf 操作线程安全
- javascript - 为什么 RxJS 操作符比 Javascript 数组方法更快地过滤和映射大尺寸数组?
- android - 在运行 lint 检测器之前收集特定的类
- pyspark - 尝试将元素插入 pyspark 数据框的数组列时出现奇怪的输出