Error Handler with Flux
Asked Answered
T

1

16

I have a React.js application that I am refactoring to use the Flux architecture, and am struggling to figure out how error handling should work while sticking to the Flux pattern.

Currently when errors are encountered, a jQuery event 'AppError' is triggered and a generic Error Handling helper that subscribes to this event puts a Flash message on the user's screen, logs to the console, and reports it via an API call. What is nice is I can trigger an error for any reason from any part of the application and have it handled in a consistant way.

I can't seem to figure out how to apply a similar paradigm with the Flux architecture. Here are the two particular scenarios I'm struggling with.

1) An API call fails

All of my API calls are made from action creators and I use a promise to dispatch an error event (IE 'LOAD_TODOS_FAILED') on failure. The store sees this event and updates it's state accordingly, but I still dont have my generic error behavior from my the previous iteration (notifications, etc).

Possible resolution:

I could create an ErrorStore that binds to the 'LOAD_TODOS_FAILED' action, but that means every time I have a new type of error, I need to explicitly add that action to the ErrorStore, instead of having all errors be automatically handled.

2) Store receives an unexpected action

This is the one I'm really confused about. I want to handle cases when an action is dispatched to a Store that does not make sense given the Store's current state. I can handle the error within the Store to clean up the state, but still may want to trigger an error that something unexpected happen.

Possible resolutions:

  1. Dispatch a new action from the store indicating the error.

    I believe Stores are not suppose to dispatch actions (let me know if I'm wrong), and I still have the same issue as with an API error above.

  2. Create a ControllerView for Error Handling that subscribes to every Store

    I could define an errors property on every store, then have a View watching every Store and only act on the errors property. When the errors property is not null, it could dispatch new actions, etc. The disadvantages are that I need to remember to add every Store to this view whenever new ones are created, and every store has to have an error property that behaves the same way. It also does nothing to address API call failures.

Does anyone have a suggested approach for a generic Error Handler that fits into the Flux architecture?

TL;DR

I need to handle errors in most Action Creators and Stores. How do I setup consistent error handling that will occur for any type of generic error?

Thorianite answered 15/1, 2015 at 5:46 Comment(0)
N
7
  1. API call fails

If you want to avoid listing every error action in the ErrorStore, you could have a generic APP_ERROR action, and have properties of that action that describe it in more detail. Then your other stores would simply need to examine those properties to see if the action is relevant to them. There is no rule that the registered callback in the stores needs to be focused on the action's type, or only on the type -- it's just often the most convenient and consistent way of determining if an action is relevant.

  1. Store receives an unexpected action

Don't issue a new action in response to an action. This results in a dispatch-within-a-dispatch error, and would lead to cascading updates. Instead, determine what action should be dispatched ahead of time. You can query the stores before issuing an action, if that helps.

Your second solution sounds good, but the dangerous thing you mentioned is "When the errors property is not null, it could dispatch new actions, etc" -- again, you don't want to issue actions in response to other actions. That is the path of pain that Flux seeks to avoid. Your new controller-view would simply get the values from the stores and respond by presenting the correct view.

Neile answered 15/1, 2015 at 17:35 Comment(2)
So I should add error handling to my action creators? Is this a common pattern? I have not seen any examples or mention of it before, but it seems reasonable. I was hoping to keep some error handling / simple business logic contained within the stores, but I guess that won't flyThorianite
I think the API errors are best encapsulated in a WebAPIUtils module. Then you can call the WebAPIUtils from the store or from the action creators and the WebAPIUtils will both make the XHR call, and then also call the right action creator in response to the result.Neile

© 2022 - 2024 — McMap. All rights reserved.