Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not handling Failure FSAs (?) #33

Closed
bd opened this issue Jan 5, 2016 · 5 comments
Closed

Not handling Failure FSAs (?) #33

bd opened this issue Jan 5, 2016 · 5 comments
Labels

Comments

@bd
Copy link

bd commented Jan 5, 2016

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.

@agraboso
Copy link
Owner

agraboso commented Jan 8, 2016

@bd In your code you don't specify what SEARCH_REQUEST, SEARCH_SUCCESS and SEARCH_FAILURE are, though I'm guessing (from your use of ${SEARCH_FAILURE} in the console.log in the second code block) that they are variables referencing strings. Maybe the two code blocks are getting different values?

@rudin
Copy link

rudin commented Feb 9, 2016

This middleware is not handling 'FAILURE' for me either.
When a request fails it's dispatching 'SUCCESS' instead, with the RequestError as payload:
RequestError {name: "RequestError", message: "Failed to fetch"}

Code:

function fetchPages(getState) {
  return {
    [CALL_API] : {
      types: [ PAGES_REQUEST, PAGES_SUCCESS, PAGES_FAILURE ],
      method: 'GET',
      endpoint: API_ROOT+'page/all',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer '+getState().auth.token
      }
    }
  }
}

Any ideas? How did you solve this @bd, or what did you start using instead?

@barrystaes
Copy link
Contributor

When a request fails it's dispatching 'SUCCESS' instead, with the RequestError as payload:

Dont you mean you get the REQUEST FSA, with the error flag?
(which may change to issuing the FAILURE FSA if we were to merge PR #26 , insights welcome)

@rudin
Copy link

rudin commented Feb 16, 2016

There's indeed an error flag. Issuing the 'FAILURE' FSA would make sense, since something surely failed. Thanks very much!

@barrystaes
Copy link
Contributor

Good.

And it turns out that the issue i linked to was actually about dropping the error flag in the REQUEST FSA and instead use a FAILURE FSA. But its something i'd like to see as in my view one REQUEST FSA should result in (only and) exactly one FSA, either SUCCESS or FAILURE. I gave the discussion on this a place in the form of a new issue #44.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants