Skip to content

Commit

Permalink
Merge pull request #4283 from bmac/commit-assert
Browse files Browse the repository at this point in the history
Add better error messaging for adapters that do not implement createR…
  • Loading branch information
pangratz authored Oct 23, 2016
2 parents b4301d8 + 22e0d21 commit 05e8436
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ Store = Service.extend({
var adapter = this.adapterFor(typeClass.modelName);

assert("You tried to find a record but you have no adapter (for " + typeClass + ")", adapter);
assert("You tried to find a record but your adapter (for " + typeClass + ") does not implement 'findRecord'", typeof adapter.findRecord === 'function' || typeof adapter.find === 'function');
assert("You tried to find a record but your adapter (for " + typeClass + ") does not implement 'findRecord'", typeof adapter.findRecord === 'function');

var promise = _find(adapter, this, typeClass, id, internalModel, options);
return promise;
Expand Down Expand Up @@ -2597,6 +2597,8 @@ function _commit(adapter, store, operation, snapshot) {
var internalModel = snapshot._internalModel;
var modelName = snapshot.modelName;
var typeClass = store.modelFor(modelName);
assert(`You tried to update a record but you have no adapter (for ${typeClass})`, adapter);
assert(`You tried to update a record but your adapter (for ${typeClass}) does not implement '${operation}'`, typeof adapter[operation] === 'function');
var promise = adapter[operation](store, typeClass, snapshot);
var serializer = serializerForAdapter(store, adapter, modelName);
var label = `DS: Extract and notify about ${operation} completion of ${internalModel}`;
Expand Down
21 changes: 21 additions & 0 deletions tests/integration/adapter/store-adapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import setupStore from 'dummy/tests/helpers/store';
import Ember from 'ember';

import {module, test} from 'qunit';
import testInDebug from 'dummy/tests/helpers/test-in-debug';

import DS from 'ember-data';

Expand Down Expand Up @@ -108,6 +109,7 @@ test("by default, createRecords calls createRecord once per record", function(as
yehuda: yehuda.save()
});
});

promise.then(assert.wait(function(records) {
tom = records.tom;
yehuda = records.yehuda;
Expand All @@ -116,6 +118,7 @@ test("by default, createRecords calls createRecord once per record", function(as
assert.asyncEqual(yehuda, store.findRecord('person', 2), "Once an ID is in, findRecord returns the same object");
assert.equal(get(tom, 'updatedAt'), "now", "The new information is received");
assert.equal(get(yehuda, 'updatedAt'), "now", "The new information is received");

}));
});

Expand Down Expand Up @@ -1400,3 +1403,21 @@ test("An async hasMany relationship with links should not trigger shouldBackgrou
assert.equal(comments.get('length'), 3);
}));
});


testInDebug("There should be a friendly error for if the adapter does not implement createRecord", function(assert) {
adapter.createRecord = null;

let tom;
assert.expectAssertion(function() {
run(function() {
tom = store.createRecord('person', { name: "Tom Dale" });
tom.save();
});
}, /does not implement 'createRecord'/);
run(function() {
// move record out of the inflight state so the tests can clean up
// correctly
store.recordWasError(tom._internalModel, new Error());
});
});

0 comments on commit 05e8436

Please sign in to comment.