From ac2206d257b39c04aaa479a8650a107a00b15f90 Mon Sep 17 00:00:00 2001 From: Ritchie Martori Date: Fri, 21 Feb 2014 09:10:48 -0800 Subject: [PATCH] Throw useful errors in DataModel stub methods --- lib/models/data-model.js | 46 ++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/lib/models/data-model.js b/lib/models/data-model.js index 3aba39d1..bb8c60cf 100644 --- a/lib/models/data-model.js +++ b/lib/models/data-model.js @@ -27,6 +27,20 @@ function setRemoting(fn, options) { } } fn.shared = true; + // allow connectors to override the function by marking as delegate + fn._delegate = true; +} + +/*! + * Throw an error telling the user that the method is not available and why. + */ + +function throwNotAttached(modelName, methodName) { + throw new Error( + 'Cannot call ' + modelName + '.'+ methodName + '().' + + ' The ' + methodName + ' method has not been setup.' + + ' The DataModel has not been correctly attached to a DataSource!' + ); } /*! @@ -58,7 +72,7 @@ function convertNullToNotFoundError(ctx, cb) { */ DataModel.create = function (data, callback) { - + throwNotAttached(this.modelName, 'create'); }; setRemoting(DataModel.create, { @@ -75,7 +89,7 @@ setRemoting(DataModel.create, { */ DataModel.upsert = DataModel.updateOrCreate = function upsert(data, callback) { - + throwNotAttached(this.modelName, 'updateOrCreate'); }; // upsert ~ remoting attributes @@ -96,7 +110,7 @@ setRemoting(DataModel.upsert, { */ DataModel.findOrCreate = function findOrCreate(query, data, callback) { - + throwNotAttached(this.modelName, 'findOrCreate'); }; /** @@ -107,7 +121,7 @@ DataModel.findOrCreate = function findOrCreate(query, data, callback) { */ DataModel.exists = function exists(id, cb) { - + throwNotAttached(this.modelName, 'exists'); }; // exists ~ remoting attributes @@ -126,7 +140,7 @@ setRemoting(DataModel.exists, { */ DataModel.findById = function find(id, cb) { - + throwNotAttached(this.modelName, 'find'); }; // find ~ remoting attributes @@ -157,7 +171,7 @@ setRemoting(DataModel.findById, { */ DataModel.find = function find(params, cb) { - + throwNotAttached(this.modelName, 'find'); }; // all ~ remoting attributes @@ -176,7 +190,7 @@ setRemoting(DataModel.find, { */ DataModel.findOne = function findOne(params, cb) { - + throwNotAttached(this.modelName, 'findOne'); }; setRemoting(DataModel.findOne, { @@ -195,7 +209,7 @@ setRemoting(DataModel.findOne, { DataModel.remove = DataModel.deleteAll = DataModel.destroyAll = function destroyAll(where, cb) { - + throwNotAttached(this.modelName, 'destroyAll'); }; /** @@ -207,7 +221,7 @@ DataModel.destroyAll = function destroyAll(where, cb) { DataModel.removeById = DataModel.deleteById = DataModel.destroyById = function deleteById(id, cb) { - + throwNotAttached(this.modelName, 'deleteById'); }; // deleteById ~ remoting attributes @@ -225,7 +239,7 @@ setRemoting(DataModel.deleteById, { */ DataModel.count = function (where, cb) { - + throwNotAttached(this.modelName, 'count'); }; // count ~ remoting attributes @@ -244,7 +258,7 @@ setRemoting(DataModel.count, { */ DataModel.prototype.save = function (options, callback) { - + throwNotAttached(this.constructor.modelName, 'save'); }; @@ -254,7 +268,7 @@ DataModel.prototype.save = function (options, callback) { */ DataModel.prototype.isNewRecord = function () { - + throwNotAttached(this.constructor.modelName, 'isNewRecord'); }; /** @@ -266,7 +280,7 @@ DataModel.prototype.isNewRecord = function () { DataModel.prototype.remove = DataModel.prototype.delete = DataModel.prototype.destroy = function (cb) { - + throwNotAttached(this.constructor.modelName, 'destroy'); }; /** @@ -280,7 +294,7 @@ DataModel.prototype.destroy = function (cb) { */ DataModel.prototype.updateAttribute = function updateAttribute(name, value, callback) { - + throwNotAttached(this.constructor.modelName, 'updateAttribute'); }; /** @@ -294,7 +308,7 @@ DataModel.prototype.updateAttribute = function updateAttribute(name, value, call */ DataModel.prototype.updateAttributes = function updateAttributes(data, cb) { - + throwNotAttached(this.modelName, 'updateAttributes'); }; // updateAttributes ~ remoting attributes @@ -313,5 +327,5 @@ setRemoting(DataModel.prototype.updateAttributes, { */ DataModel.prototype.reload = function reload(callback) { - + throwNotAttached(this.constructor.modelName, 'reload'); };