Skip to content

Not handling Failure FSAs (?)  #33

@bd

Description

@bd

Most likely I'm misinterpreting the docs, but here's my problem:

I would like to handle failed api requests in a similar way to how I handle the successful api requests. Yet as far as I can tell, the SEARCH_FAILURE FSA never gets handled, though it is treated (AFAICT) identically to the SEARCH_SUCCESS FSA. It does seem to be created and dispatched, based on what I see in the devtools.

I have this

import { CALL_API } from 'redux-api-middleware'
import { handleActions } from  'redux-actions'
const searchReducer = handleActions({

 SEARCH_SUCCESS: (state = defaultState, action) => {
     return {
   ...state,
    search_results: ({...action.payload}),
    api: {
        requestPending: false,
        searchPending: false
    },
  }
 },

SEARCH_FAILURE:  function(state = defaultState, action) {
    console.log("Handling SEARCH_FAILURE given state, action: ", state, action)

    return {
            ...state,
            search_results: {Total: 0},
            api: {
                requestPending: false,
                error: action.payload
                },
            errors: [action.payload, ...state.errors]
        }
},
})

the SEARCH_SUCCESS FSA gets handled by searchReducer, but when the server gives a 400 response, the SEARCH_FAILURE handler never gets called--at least I don't see the log output I would expect, and the state sure doesn't end up looking right. I do see a SEARCH_FAILURE entry in the redux devtools panel, however.

Serving to confuse me further, here is the declaration I have at the moment for creating the RSAA

export function doSearch( selected_filters, page ){
let qs = SearchPage.constructQueryString(selected_filters, page)

return {
  [CALL_API]: {
  endpoint: `/api/songs/search?${qs}`,
  method: 'GET', 
  types: [
        {type: SEARCH_REQUEST},
        {type: SEARCH_SUCCESS},
        {
           type: SEARCH_FAILURE,
           payload: (action, state, res) => {
                 if (400 === res.status)
                  {
                     console.log(`${SEARCH_FAILURE} payload: `, action, state, res)
                  }
                 return res
              }
            },
         ],
      headers: { 'Content-Type': 'application/json' },
      credentials: 'include'
     }
  }
}

the payload function is being called and logging more or less what I'd expect. So what am I messing up here? As far as I can discern from multiple readings of the docs for redux-api-middleware, this setup should yield the behavior I want, but it does not. The successes succeed, but the failures fail...

And I'm posting here because it seems like this is either a problem with in the middleware code, or ultimately caused by confusing/sparse language in the docs. I'm happy to post it to stackoverflow or whereever would be more appropriate if this is the wrong area.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions