Bloc: Error handling in a BLoC if state does not change

Created on 14 May 2019  路  4Comments  路  Source: felangel/bloc

Hi,

First of all, thanks for this great lib! It's very easy to use, the documentation is extensive and the examples help a lot to get started.

I have some questions about error handling in the todos example app. Let's suppose that the operations in the repository layer can fail and we want to catch those errors and show some message in the app. For example: the UI layer dispatches an Addevent but the repository layer throws an exception. In the current version of the app the _saveTodos is called after yielding a new Loadedstate. Let's say we want to yield a new state only if the _saveTodoscall is successful so we move that call before the yield.

My question is that how should we handle exceptions thrown by _saveTodos? The state of the bloc does not change in this case so there is nothing to yield. How should the UI layer be notified about the error? BlocDelegate(onError) does not seem to be a good choice or am I wrong? Do you have any suggestions?

question

Most helpful comment

Hi @felangel

Thanks for your help, I'll continue experimenting.

All 4 comments

Hi @andrasgyenes 馃憢
Thanks for opening an issue and for the positive feedback!

Regarding your question, you can restructure the method to await _saveTodos and wrap it in a try/catch like:

try {
  ...
  await _saveTodos();
  yield Success();
} catch (error) {
  yield Failure(error);
}

Then in the UI layer you can handle the failure state.
Hope that helps and great question 馃憤

Hi @felangel

Thanks for the quick reply! I forgot to mention that the VS Code plugin is very helpful too :-)

Assume we have 5 todo items so that current state is Loaded. The user enters a new item, dispatch(AddTodo) is called from onSave but the operation fails. We still have 5 successfully loaded items in the bloc so I think the state is more like Loaded than Failure.

I can think of the following solutions:
1) Add a new Failure state like you suggested. In this case the state should contain the loaded items as well otherwise the list would be empty.
2) Keep the Loaded state but add a new 'last error' member to it.
3) Maybe introduce a new bloc for implementing the adding logic?

Do you have any other ideas?

Hi @andrasgyenes
No problem 馃憤

In that case I think either option 1 or 2 should be fine. I would recommend trying each out and go with the one you prefer. The Bloc library has no opinion on how you should represent your events and states so it's up to you as a developer to design them for your feature set 馃槃

I think the main advantage of 2 over 1 is that you can implement a copyWith method to conveniently clone the state and modify either the loaded items or the error without needing to pass them explicitly.

Hope that helps and great question 馃挴

Hi @felangel

Thanks for your help, I'll continue experimenting.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

craiglabenz picture craiglabenz  路  3Comments

1AlexFix1 picture 1AlexFix1  路  3Comments

wheel1992 picture wheel1992  路  3Comments

clicksocial picture clicksocial  路  3Comments

ricktotec picture ricktotec  路  3Comments