ebryn / ember-model

A lightweight model library for Ember.js
MIT License
524 stars 159 forks source link

Better Library Errors #365

Open narkeeso opened 10 years ago

narkeeso commented 10 years ago

I've been working heavily ember-model (store-less version) for the past 6-8 months and have learned a great deal. I thought that it might be good to find points where ember-model might fail due to user error.

For example I constantly run into issues with HasManyEmbedded objects where I am incorrectly setting properties on a model and the model fails to materialize. The most common error I see with ember-model is:

Uncaught TypeError: Cannot read property 'reference' of undefined 

It's when the model is attempting to materialize:

Ember.EmbeddedHasManyArray = Ember.ManyArray.extend({
  materializeRecord: function(idx) {
    var klass = get(this, 'modelClass'),
        primaryKey = get(klass, 'primaryKey'),
        content = get(this, 'content'),
        reference = content.objectAt(idx),
        attrs;

    if (reference !== undefined) {
      attrs = reference.data;
    } else {
      throw new Error('There was a problem with materializeRecord() on ' +
        klass);
    }

    if (reference.record) {
      return reference.record;
    } else {
      var record = klass.create({ _reference: reference });
      reference.record = record;
      if (attrs) {
        record.load(attrs[primaryKey], attrs);
      }
      return record;
    }
  },
});

Above I added a check on the reference and to throw an error with the model with an issue so I know exactly where to start looking. Is this the best way to go about better informing the user?