ember-model icon indicating copy to clipboard operation
ember-model copied to clipboard

Better Library Errors

Open narkeeso opened this issue 11 years ago • 0 comments

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?

narkeeso avatar May 10 '14 01:05 narkeeso