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

The exception does not work within the reaction #853

Open
kirill09 opened this issue Aug 22, 2022 · 2 comments
Open

The exception does not work within the reaction #853

kirill09 opened this issue Aug 22, 2022 · 2 comments
Assignees

Comments

@kirill09
Copy link

Hi. Found an interesting bug. If method which processes reaction is not async then exception is not displayed in console and Guarded doesn't work.

reaction((_) => name, validateUsername),
....
// No exception
@action
void validateUsername(String value) {
  print('validateUsername');
  throw Exception('Test exception');
}

// Exception works
@action
Future<void> validateUsernameAsync(String value) async {
  print('validateUsernameAsync');
  throw Exception('Test exception');
}

I did not find any mention of this feature in the documentation. Why does this happen and is this a bug or normal? I think this should be described in the documentation

@amondnet
Copy link
Collaborator

amondnet commented Oct 3, 2022

@kirill09
https://mobx.netlify.app/api/context#reactiveconfig

disableErrorBoundaries: When true, MobX will not guard against exceptions thrown during reactions. By default, it is false, which means MobX will catch unhandled exceptions and ensure the consistency of the reactive system.

if (_context.config.disableErrorBoundaries == true) {

https://mobx.js.org/configuration.html#disableerrorboundaries-boolean

By default, MobX will catch and re-throw exceptions happening in your code to make sure that a reaction in one exception does not prevent the scheduled execution of other, possibly unrelated, reactions. This means exceptions are not propagated back to the original causing code and therefore you won't be able to catch them using try/catch.

By disabling error boundaries, exceptions can escape derivations. This might ease debugging, but might leave MobX and by extension your application in an unrecoverable broken state. Default: false.

test('reaction throws when config.disableErrorBoundaries = true', () {

I think mobx should not throw exception in reaction. Even if it is an async action, mobx should not throw an exception. And if you need error handling, you can use onError .

reaction((_) => name, validateUsername, onError: (e, r) {
      error = e;
      print('error');
    }),

@amondnet
Copy link
Collaborator

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

No branches or pull requests

2 participants