Skip to content

Commit

Permalink
Add better error messaging for adapters that do not implement createR…
Browse files Browse the repository at this point in the history
…ecord, updateRecord or deleteRecord.
  • Loading branch information
bmac committed Aug 28, 2016
1 parent 5d9b210 commit 22e0d21
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 @@ -680,7 +680,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 @@ -2425,6 +2425,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 22e0d21

Please sign in to comment.