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

Question: Catching Errors #46

Closed
ksloan opened this issue Mar 17, 2017 · 6 comments
Closed

Question: Catching Errors #46

ksloan opened this issue Mar 17, 2017 · 6 comments

Comments

@ksloan
Copy link

ksloan commented Mar 17, 2017

I'm having trouble with errors being swallowed and never bubbling up. I can't seem to use the promise .catch(), or wrap in a try/catch.

Specifically, when I dispatch an action that results in a state change, React re-renders my components. If React throws an error during the render method I can't seem to get ahold of it.

I don't know much about RxJS/Observables... but I can see that my error is being caught by RxJS here https://github.com/ReactiveX/rxjs/blob/master/src/Subscriber.ts#L260-L265 and then I don't know where it goes. I'm assuming redux-logic should throw the error being passed back by RxJS, or use the failType or UNHANDLED_LOGIC_ERROR action types to someone notify me of this error.

As it stands now... it's just silent and everything stops. Any help, or an explanation would be helpful. What is the proper way to handle errors like this with redux-logic?

Example code:

export const someLogic = createLogic({
  type: ACTION_TYPES.LOGIC_EXAMPLE,
  process({ getState, action }, dispatch, done) {
    axios.get('/data')
      .then(res => {
        dispatch(setUser({ user: {name: 'crash render()'} }))
      }).catch(err => {
        // never gets called
        console.error(err)
      }).then(() => done())
  }
})

Thanks!

@jeffbski
Copy link
Owner

That is wierd. Yes, you normally get the error just like you are doing with the catch handler where you can log it and possibly dispatch something else.

Here is an jsfiddle where I do a similar thing in code:

https://jsfiddle.net/jeffbski/78vpf92k/

If you change the URL to the wrong address (like adding BAD to the beginning of the server name) then when you try to search it will correctly log the error and display the error message.

If you have a github repo where I can look at all of the code, maybe I can help you figure out what is going on.

@ksloan
Copy link
Author

ksloan commented Mar 17, 2017

I wrote up a minimal fiddle to demonstrate what I'm talking about.

Just to show you that it all works, this is a working version that doesn't throw any errors https://jsfiddle.net/ksloan/x4khtjtj/2/ When you click the button the state changes, and the App component gets re-rendered.

Now if we set the attribute to an object instead of a string, render() will throw, but we won't see the error, everything just stops. The only difference is line 23. https://jsfiddle.net/ksloan/x4khtjtj/3/

Please let me know if you are seeing the same thing I am, or if I'm doing something wrong.

@jeffbski
Copy link
Owner

It looks like if the error occurs in the reducer then it is getting caught by RxJS and hidden.

If the errors occur outside of the dispatch then they are working properly.

I'll take a look at the code and see if I can fix this. Thanks for bringing my attention to this.

@jeffbski
Copy link
Owner

I was able to add a test to cover this. Basically now it will console.error any error that occurs in dispatching (or the reducer). Errors that occur during dispatching are especially bad since we don't know that we can safely do any additional dispatching since the reducer is questionable. If we try to do any additional dispatching in that unknown state we could easily get into an infinite loop. Thus the best thing I could think of doing at this point is to console.error the err. I have just published this in v0.11.9.

@jeffbski jeffbski added the bug label Mar 17, 2017
@ksloan
Copy link
Author

ksloan commented Mar 17, 2017

Great, that will help a lot. Thanks!

@jeffbski
Copy link
Owner

jeffbski commented Mar 17, 2017

Also if for testing purposes, the logicMiddleware.monitor$ now has a new op: 'dispatchError' that will be emitted with the error.

So we can verify exactly what happened. You can see this commit if interested.

e50e3bd

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

2 participants