martyjs / marty

A Javascript library for state management in React applications
http://martyjs.org
MIT License
1.09k stars 77 forks source link

Properly surface React components rendering exceptions when using Marty container wrappers #295

Closed goldensunliu closed 9 years ago

goldensunliu commented 9 years ago

I have noticed that whenever hasChanged() is fired from a Marty store, rendering exceptions thrown from listening containers' components always are thrown on hasChanged().

Here is an example: Trying to surface the React component error using marty container, here is a contrived view that will also throw error on render

var MyBadUsersView = React.createClass({
  render: function () {
    throw new Error('crap');
  }
});

module.exports = MyBadUsersView

this is its container which listen to changes on UserStore

var UserStore = require('stores/UserStore');
var MyBadUsersView = require('components/MyBadUsersView');

UsersContainer = Marty.createContainer(MyBadUsersView, 
  {
    listenTo: [UserStore],
    fetch: {
      users: function() {
        UserStore.getAllUsers();
      }
    }
  }
);

Here is the store which triggers this.hasChanged() on UserConstans.RECEIVE_USER

var UserConstants = require('constants/UserConstants');

var UserStore = Marty.createStore({
  id: 'UserStore',

  getInitialState: function() {
    { users: { } }
  },

  handlers: {
    addUser: UserConstans.RECEIVE_USER
  },

  getAllUsers: function() {
    this.fetch({
      id: 'getAllUsers',
      locally:  function() {
        return this.state.users
      }
    })
  },

  addUser:  function(user) {
    this.state.users[user.id] = user;
    // This is where the rendering exception is thrown
    try {
      this.hasChanged();
    } catch (error) {
      // Error('crap') is caught here
      console.log(error);
    }
  }
});

As you can use the rendering exception thrown in the component surfaces when this.hasChanged() is called in UserStore.

My question is: Is there anyways for React components within a Marty Container to surface their rendering exceptions to top of the application?

taion commented 9 years ago

Resolved by #320.