简体   繁体   English

redux-saga catch无法加载资源:net :: ERR_CONNECTION_REFUSED

[英]redux-saga catch Failed to load resource: net::ERR_CONNECTION_REFUSED

I use axios to make call to the backend server. 我使用axios拨打后端服务器的电话。 And with redux-saga I can control the side effect from the server easily. 借助redux-saga我可以轻松控制服务器的副作用。

import {call, put, takeEvery} from "redux-saga/effects";
import {REQUEST_FAILED, REQUEST_SUCCESS, ROOT_URL, SUBMIT_USERNAME_PASSWORD} from "../../constants";
import axios from "axios/index";

const shootApiTokenAuth = (values) => {
  const {username, password} = values;
  return axios.post(`${ROOT_URL}/api-token-auth/`,
    {username, password});
};

function* shootAPI(action) {
  try {
    const res = yield call(shootApiTokenAuth, action.payload);
    const {history} = action.payload;
    yield put({
      type: REQUEST_SUCCESS,
      payload: res
    });
    history.push('/companies'); //push user to `/companies` page
  } catch (err) {
    yield put({
      type: REQUEST_FAILED,
      payload: err
    });
  }
}

export function* watchSubmitBtn() {
  yield takeEvery(SUBMIT_USERNAME_PASSWORD, shootAPI);
}

Problem: When the backend server is down. 问题:后端服务器关闭时。 It does not return any error back to me. 它不会向我返回任何错误。 And browser will raises an error Failed to load resource: net::ERR_CONNECTION_REFUSED 并且浏览器将引发错误Failed to load resource: net::ERR_CONNECTION_REFUSED

I have seen previous answer on callback method , but I prefer axios and redux-saga not callback 我已经看到了有关回调方法的先前答案,但我更喜欢axiosredux-sagacallback

I had tried console.log(err) . 我尝试过console.log(err) And I still searching they way to grab the error message and differentiate it from server response error. 而且我仍在搜索它们以获取错误消息并将其与服务器响应错误区分开的方法。

Error: Network Error
    at createError (createError.js:16)
    at XMLHttpRequest.handleError (xhr.js:87)

Question: 题:
How can I use axios and redux-saga to catch the err ? 如何使用axiosredux-saga捕获err

If you put the try/catch around the axios request itself, then you can get a bit more granularity on the cause. 如果将try / catch放在axios请求本身周围,则可以更详细地了解原因。

https://gist.github.com/fgilio/230ccd514e9381fafa51608fcf137253 https://gist.github.com/fgilio/230ccd514e9381fafa51608fcf137253

You probably want to have a custom error format and an error reducer that handles the different types of errors appropriate. 您可能希望拥有一个自定义错误格式和一个错误减少程序,以处理各种不同类型的错误。 For example if you got a response you could parse it and add it to the error, else you know there is an application level error which you would handle with an 'Oops' page or something like that. 例如,如果收到响应,则可以将其解析并将其添加到错误中,否则您将知道将使用“糟糕”页面或类似内容来处理应用程序级错误。

case REQUEST_FAILED:
      //Probably it can failed by 2 reason
      //1. 404 from server
      //2. network is down
      if (action.payload.response === undefined) {
        return {
          token: undefined,
          message: 'Network is down',
          isAuthenticated: false,
          statusCode: 406
        }
      } else {
        const tmp = action.payload.response.request.response;
        const tmp2 = JSON.parse(tmp);
        return {
          token: undefined,
          message: tmp2.non_field_errors[0],
          isAuthenticated: false,
          statusCode: action.payload.response.status
        };
      }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AxiosError:加载资源失败:net::ERR_CONNECTION_REFUSED - AxiosError : Failed to load resource: net::ERR_CONNECTION_REFUSED Webkit浏览器无法加载资源:net :: ERR_CONNECTION_REFUSED - Webkit Browsers Failed to load resource: net::ERR_CONNECTION_REFUSED 发出GET请求时出错“无法加载资源:net :: ERR_CONNECTION_REFUSED” - error when making GET request “Failed to load resource: net::ERR_CONNECTION_REFUSED” 从本地主机获取资源失败:net :: ERR_CONNECTION_REFUSED - Failed to load resource: net::ERR_CONNECTION_REFUSED when fetching from localhost 无法加载资源:Net::ERR_CONNECTION_REFUSED on NodeJs ,AngularJs Web 应用程序 - Failed to load resource: net::ERR_CONNECTION_REFUSED on NodeJs ,AngularJs Web Application 在看似没有改变之后,正在工作的 function 现在正在加载资源失败:net::ERR_CONNECTION_REFUSED - After changing seemingly nothing, a function that was working is now getting Failed to load resource: net::ERR_CONNECTION_REFUSED 无法加载资源:net::ERR_CONNECTION_REFUSED 仅适用于来自 instagram API 的选择性图像 - Failed to load resource: net::ERR_CONNECTION_REFUSED for only selective images from instagram API 这个问题的解决方案是什么“加载资源失败:net::ERR_CONNECTION_REFUSED http://localhost:8989/route?.....” - what is the solution to this problem "failed to load resource: net::ERR_CONNECTION_REFUSED http://localhost:8989/route?....." Heroku + Node.js + Peer.js(webrtc):无法加载资源:net :: ERR_CONNECTION_REFUSED - Heroku + Node.js + Peer.js (webrtc): Failed to load resource: net::ERR_CONNECTION_REFUSED 如何捕获.net::ERR_CONNECTION_REFUSED - How to catch net::ERR_CONNECTION_REFUSED
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM