【问题标题】:How to globally redirect error 500 to a page using react-redux如何使用 react-redux 将错误 500 全局重定向到页面
【发布时间】:2023-03-17 14:18:01
【问题描述】:

我正在使用这个反应 redux starter,我想知道如何将所有 api 500 错误重定向到一个页面?

【问题讨论】:

  • 你能捕捉到前端的错误吗?如果可以,请使用您想要直接访问的页面推送到历史记录。你可以在 react-router 的文档中查看实现细节。

标签: reactjs express react-redux http-status-code-500


【解决方案1】:

redux-starter 默认安装了react-router-reduxredux-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'));
    }
  });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2018-11-27
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    相关资源
    最近更新 更多