redux-promise with Axios, and how do deal with errors?

后端 未结 6 1652
梦如初夏
梦如初夏 2020-12-16 02:07

So, I see on an error, redux-promise hands me back error: true, along with the payload, but that is once it hits the reducer... to me, decoupling the request AND error condi

6条回答
  •  盖世英雄少女心
    2020-12-16 02:31

    Still using redux-promises you can do something like this which I think is an elegant way to deal with this problem.

    First, set a property in the redux state that will hold any ajax errors that may occurred.

    ajaxError: {},
    

    Second, setup a reducer to handle ajax errors:

    export default function ajaxErrorsReducer(state = initialState.ajaxError, action) {
      if (action.error) {
        const { response } = action.payload;
        return {
          status: response.status,
          statusText: response.statusText,
          message: response.data.message,
          stack: response.data.stack,
        };
      }
      return state;
    }
    

    Finally, create a very simple react component that will render errors if there are any (I am using the react-s-alert library to show nice alerts):

    import React, { Component } from 'react';
    import { connect } from 'react-redux';
    import PropTypes from 'prop-types';
    import Alert from 'react-s-alert';
    
    class AjaxErrorsHandler extends Component {
    
      constructor(props, context) {
        super(props, context);
        this.STATUS_GATE_WAY_TIMEOUT = 504;
        this.STATUS_SERVICE_UNAVAILABLE = 503;
      }
    
      componentWillReceiveProps(nextProps) {
        if (this.props.ajaxError !== nextProps.ajaxError) {
          this.showErrors(nextProps.ajaxError);
        }
      }
    
      showErrors(ajaxError) {
        if (!ajaxError.status) {
          return;
        }
        Alert.error(this.getErrorComponent(ajaxError), {
          position: 'top-right',
          effect: 'jelly',
          timeout: 'none',
        });
      }
    
      getErrorComponent(ajaxError) {
        let customMessage;
        if (
          ajaxError.status === this.STATUS_GATE_WAY_TIMEOUT ||
          ajaxError.status === this.STATUS_SERVICE_UNAVAILABLE
        ) {
          customMessage = 'The server is unavailable. It will be restored very shortly';
        }
        return (
          

    {ajaxError.statusText}

    {customMessage ? customMessage : ajaxError.message}
    ); } render() { return (
    ); } } AjaxErrorsHandler.defaultProps = { ajaxError: {}, }; AjaxErrorsHandler.propTypes = { ajaxError: PropTypes.object.isRequired, }; function mapStateToProps(reduxState) { return { ajaxError: reduxState.ajaxError, }; } export default connect(mapStateToProps, null)(AjaxErrorsHandler);

    You can include this component in your App component.

提交回复
热议问题