0

I'm trying "redux-promise". When there's no error in the flow, my code works properly. But, let's say that the API is down or I have a typo in the URL. In those cases, I expect to handle the error in the proper way.

This is the API: https://jsonplaceholder.typicode.com/users (in the snippet I'm adding random text at the end to produce the 404)

Action creator

export async function fetchUsers() {

  const request = await axios
    .get('https://jsonplaceholder.typicode.com/userssdfdsfdsf')
    .catch(error => console.log('ERROR', error))

  return {
    type: FETCHING_USERS,
    payload: request
  };
}

Reducer

export default (state = [], action) => {
  switch (action.type) {
    case FETCHING_USERS:
      return [...state, ...action.payload.data]

    default:
      return state
  }
}

I can see the error logged in the console

ERROR Error: Request failed with status code 404

But, once the action is dispatched its payload is undefined

action {type: "FETCHING_USERS", payload: undefined}

I don't know where is the best place to handle this: action creator, reducer, etc. I shouldn't check if payload is something in the reducer and, if not, return state or do nothing. I want to understand which would be the best approach to handle this.

Peter
  • 2,004
  • 2
  • 24
  • 57

3 Answers3

0

You may look at source of redux-promise, as it very simple.

Redux-promise expects either promise or action with payload set to some promise. I think you're going to use leter case.

Code may look like (just example, not tested):

export function fetchUsers() {

    const request = axios.get('https://jsonplaceholder.typicode.com/userssdfdsfdsf');

    return {
        type: FETCHING_USERS,
        payload: request
    };
}

In this case redux-promise will await for resolution of promise returned by axios.get and dispatch your action but payload replaced with promise result. In case of error, redux-promise will catch it and dispatch action with error = true (you may want to handle action.error === true case in reducer)

Fyodor Yemelyanenko
  • 11,264
  • 1
  • 30
  • 38
0

In the reducer you should check for the existence of the error field in action:

export default function(state = null, action) {
  if (action.error) {
    //handle
  }  
  switch(action.type) {
varoons
  • 3,807
  • 1
  • 16
  • 20
0

This code is for the action.

export const fetchUsers = () => async dispatch => { try { const res = await axios.get('https://jsonplaceholder.typicode.com/userssdfdsfdsf'); dispatch({ type: FETCHING_USERS, payload: res.data }); } catch (err) { dispatch({ type: FETCHING_ERROR }); } };

In reducer do this. Define the initial state and use this code.

export default function(state = initialState, action) {
  const { type, payload } = action;

  switch (type) {
    case FETCHING_USERS:
      return {
        ...state,
        loading: false,
        user: payload,

      };
case FETCH_ERROR:
      return {
        ...state,
        token: null,
        loading: false,
      };