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

Dispatch failure FSA when fetch fails #175

Merged
merged 2 commits into from
Jun 10, 2018
Merged

Dispatch failure FSA when fetch fails #175

merged 2 commits into from
Jun 10, 2018

Conversation

nason
Copy link
Collaborator

@nason nason commented Mar 3, 2018

Previously:

RSAA -> Request FSA -> Request Error FSA

Now:

RSAA -> Request FSA -> Failure FSA

Closes #26 #44 #99

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling fda1dc5 on failed-req-failure-fsa into af94473 on master.

@coveralls
Copy link

coveralls commented Mar 3, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling 96b0691 on failed-req-failure-fsa into 836a3d3 on next.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling fda1dc5 on failed-req-failure-fsa into af94473 on master.

@darthrellimnad
Copy link
Contributor

First off, I love this lib! 🎉

Second, I personally welcome this change :). Checking for the .error flag on the REQUEST action was always kinda a pain and source of confusion for new developers.

However, this is a pretty big breaking change, since now FAILURE actions can return the the RequestError type, whereas (i think) this wasn't possible before. Depending on the project, this could be significant?

do you think there would be a way to support a "classic" mode of sorts? I wouldn't want it included if it added a lot of overhead, but might help with migration from 2 -> 3 among community :)

@@ -160,7 +160,7 @@ function apiMiddleware({ getState }) {
return next(
await actionWith(
{
...requestType,
...failureType,
Copy link
Contributor

@darthrellimnad darthrellimnad Apr 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a "migration" helper option could exist? like:

   ...(options.legacyRequestError ? requestType : failureType),

maybe until v4 or something, although not sure if anyone else might find this useful, can always stick with v2 till you're ready for migration too :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm open to a migration helper, but I don't know what the right approach is here. A breaking change that:

  • requires you to go in and add a legacyRequestError: true to your RSAAs as a temporary fix

OR

  • requires you to update your error handling paths to this new spec

I lean towards the latter but can be convinced otherwise!

@craig0990
Copy link

craig0990 commented May 2, 2018

OK, I've just bumped into this on my first run-through with the library (because my API isn't accessible right this second - I figured I'd boot it up when I got the code working).

Obviously, things are fine as they are right now (now that I'm checking for .error in REQUEST-type actions), but it was confusing to begin with 😀

Just adding my tuppence, and eagerly awaiting 3.0 🙃

Edit: just reading through the Lifecyle in the README, and while in a lot of cases it would be sufficient to dispatch a FAILURE FSA, perhaps there's a case for an ERROR FSA, to distinguish between where the API was actually contacted, but failed, and where the API couldn't be called at all.

I can see us potentially using re-dispatching an ERROR FSA so we could gracefully handle offline functionality, for example.

@darthrellimnad
Copy link
Contributor

darthrellimnad commented May 3, 2018

Agree :) although as things stand, the Request FSA w/ error flag should indicate the "failed to contact API" case? So with the change from the PR, seems like we would need to test the payload for the error types (RequestError vs ApiError... or InternalError I guess, but those can be avoided).

What would a test look like for the new Failure FSA case for RequestError? Would we do a payload instanceof RequestError?

EDIT: looks like we could also use the name property of the error payload of the "FAILURE' FSA to switch around the error type as well:
https://github.com/agraboso/redux-api-middleware#requesterror

Copy link
Collaborator Author

@nason nason left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been a bit distracted but want to pick this back up and get this change out!

Thanks for the great feedback @craig0990 + @darthrellimnad. Can you share any idea or samples of how you think this PR could be extended to give more information about what type of error happened?

@@ -160,7 +160,7 @@ function apiMiddleware({ getState }) {
return next(
await actionWith(
{
...requestType,
...failureType,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm open to a migration helper, but I don't know what the right approach is here. A breaking change that:

  • requires you to go in and add a legacyRequestError: true to your RSAAs as a temporary fix

OR

  • requires you to update your error handling paths to this new spec

I lean towards the latter but can be convinced otherwise!

@nason nason changed the base branch from master to next June 10, 2018 16:36
Previously:

RSAA -> Request FSA -> Request Error FSA

Now:

RSAA -> Request FSA -> Failure FSA

Closes #26 #44 #99
@nason
Copy link
Collaborator Author

nason commented Jun 10, 2018

Merging this into the next branch for a 3.0 release. Open to tweaking this braking change further and adding different migration paths -- feel free to comment here or open PRs!

@nason nason merged commit a0460d2 into next Jun 10, 2018
@craig0990
Copy link

I can't quite remember exactly what I was thinking when I wrote the earlier comment :P I think it was around the fact that my API code had failed, but I had naively expected the FAILURE case to be called, and it wasn't.

A loading key as part of my reducers is one of my main use-cases. With the current implementation, I have to catch errors in two places.

Here's an example of me handling the REQUEST action to account for that:

      REQUEST: (state, action) => {
        return {
          ...state,
          meta: action.meta,
          // Requests may error in certain situations
          error: action.error,
          // We need to stop loading once we detect an error
          loading: !action.error,
          initialised: true
        }

By having the FAILURE FSA dispatched, I'd be able to simplify my code by explicitly setting loading to true on REQUEST, and false on FAILURE, regardless of whether the error was actually caused by mis-configuration or a bad response.

Hope that clarifies :)

@nason
Copy link
Collaborator Author

nason commented Jun 11, 2018

@craig0990 thanks. I hope this change makes exactly that possible! It's currently in the next branch, from which I'm hoping to cut a 3.0 beta very soon.

I'll leave an update here when that happens 🍎

@nason
Copy link
Collaborator Author

nason commented Jun 14, 2018

This has been released in 3.0.0-beta.0

@nason nason deleted the failed-req-failure-fsa branch June 17, 2018 05:31
nason added a commit that referenced this pull request Jul 16, 2018
This is a follow-up to #175, which updated this behavior and docs
for failed fetches, to include failures in RSAA custom methods,
such as `[RSAA].headers`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants