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

Feature request: dispatch an _ERROR action if there was an API error #17

Open
shankie-codes opened this issue Jul 18, 2017 · 9 comments

Comments

@shankie-codes
Copy link

You're going to love me @mikestead .

When I've used Redux before, one of the starters that I used had a really nice model, that was, when making AJAX requests, to dispatch _REQUEST, _SUCCESS and _FAILURE actions depending on what happened in the api service. Originally I thought that this was daft, but actually, you want to be able to handle errors in different kinds of requests differently.

It looks like it should be a pretty easy change as you'll just have to add a .catch() on the dispatch around here – and dispatch a new action, such MY_ACTION_NAME_ERROR. If I'm not mistaken, makeFetchRequest already returns a fetch promise, which should already reject if there's an error, which means that there shouldn't be too much working through the entire codebase.

Are my assumptions above correct? If so, would you accept a PR for that?

@shankie-codes
Copy link
Author

(Disclaimer – I don't know TypeScript, but this looks like a pretty vanilla JS change anyways)

@shankie-codes
Copy link
Author

Oh and obvs I could do this in my reducer, but I think it's tidier to do all the main switching on actions.

@shankie-codes
Copy link
Author

Quick bump on this @mikestead – thoughts?

@mikestead
Copy link
Owner

mikestead commented Aug 31, 2017

@shankiesan I can see the angle you're coming from but I'm not totally sure about this one.

As we always dispatch a FSA you can add middleware to inspect all actions for errors. From there you could siphon off more specific actions as required.

An aim here is to give some basic wiring but to also keep things light and not get too opinionated around how Redux has been setup. Not super easy, but was one of the reasons I went with FSA as it was well established.

@shankie-codes
Copy link
Author

Fair enough @mikestead . As you say, adding middleware to inspect all actions for errors is a good idea. I'll run with that. Cheers

@jedrichards
Copy link

The middleware/api actions in the official real world Redux example uses the _REQUEST, _SUCCESS and _FAILURE pattern. For my part I think this is more idiomatic Redux / best practice than the FSA format, which I haven't really seen get wide spread adoption in the wild.

https://github.com/reactjs/redux/blob/master/examples/real-world/src/actions/index.js

Just my 2 cents worth :)

@shankie-codes
Copy link
Author

Hey @mikestead I only just noticed @jedrichards comment. This is basically my feeling as well. I appreciate the FSA approach, but I think the idiomatic Redux approach would be more suitable.

How about a CLI flag for the action type? Obviously this is a major change, but something like --action-type=FSA or --action-type=redux would be pretty sweet.

@mikestead
Copy link
Owner

@shankie-san @jedrichards Thanks for the feedback. Yeah a flag to opt into it sounds good. I'll reopen the ticket.

I'd probably want to keep the current default to be backwards compatible. Don't have time to look at this anytime soon I'm afraid.

@shankie-codes
Copy link
Author

Yep that makes sense to leave the current as the default @mikestead

I'd be happy to help add this when we've got a new project that we're going to base on OpenAPI. Maybe @jedrichards and I could take a look at it? Anyways yes let's leave it open in the hopes that one of us has a project that makes it worthwhile.

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

No branches or pull requests

3 participants