Throw useful errors in DataModel stub methods

This commit is contained in:
Ritchie Martori 2014-02-21 09:10:48 -08:00
parent 64b374907a
commit ac2206d257
1 changed files with 30 additions and 16 deletions

View File

@ -27,6 +27,20 @@ function setRemoting(fn, options) {
} }
} }
fn.shared = true; 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) { DataModel.create = function (data, callback) {
throwNotAttached(this.modelName, 'create');
}; };
setRemoting(DataModel.create, { setRemoting(DataModel.create, {
@ -75,7 +89,7 @@ setRemoting(DataModel.create, {
*/ */
DataModel.upsert = DataModel.updateOrCreate = function upsert(data, callback) { DataModel.upsert = DataModel.updateOrCreate = function upsert(data, callback) {
throwNotAttached(this.modelName, 'updateOrCreate');
}; };
// upsert ~ remoting attributes // upsert ~ remoting attributes
@ -96,7 +110,7 @@ setRemoting(DataModel.upsert, {
*/ */
DataModel.findOrCreate = function findOrCreate(query, data, callback) { 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) { DataModel.exists = function exists(id, cb) {
throwNotAttached(this.modelName, 'exists');
}; };
// exists ~ remoting attributes // exists ~ remoting attributes
@ -126,7 +140,7 @@ setRemoting(DataModel.exists, {
*/ */
DataModel.findById = function find(id, cb) { DataModel.findById = function find(id, cb) {
throwNotAttached(this.modelName, 'find');
}; };
// find ~ remoting attributes // find ~ remoting attributes
@ -157,7 +171,7 @@ setRemoting(DataModel.findById, {
*/ */
DataModel.find = function find(params, cb) { DataModel.find = function find(params, cb) {
throwNotAttached(this.modelName, 'find');
}; };
// all ~ remoting attributes // all ~ remoting attributes
@ -176,7 +190,7 @@ setRemoting(DataModel.find, {
*/ */
DataModel.findOne = function findOne(params, cb) { DataModel.findOne = function findOne(params, cb) {
throwNotAttached(this.modelName, 'findOne');
}; };
setRemoting(DataModel.findOne, { setRemoting(DataModel.findOne, {
@ -195,7 +209,7 @@ setRemoting(DataModel.findOne, {
DataModel.remove = DataModel.remove =
DataModel.deleteAll = DataModel.deleteAll =
DataModel.destroyAll = function destroyAll(where, cb) { DataModel.destroyAll = function destroyAll(where, cb) {
throwNotAttached(this.modelName, 'destroyAll');
}; };
/** /**
@ -207,7 +221,7 @@ DataModel.destroyAll = function destroyAll(where, cb) {
DataModel.removeById = DataModel.removeById =
DataModel.deleteById = DataModel.deleteById =
DataModel.destroyById = function deleteById(id, cb) { DataModel.destroyById = function deleteById(id, cb) {
throwNotAttached(this.modelName, 'deleteById');
}; };
// deleteById ~ remoting attributes // deleteById ~ remoting attributes
@ -225,7 +239,7 @@ setRemoting(DataModel.deleteById, {
*/ */
DataModel.count = function (where, cb) { DataModel.count = function (where, cb) {
throwNotAttached(this.modelName, 'count');
}; };
// count ~ remoting attributes // count ~ remoting attributes
@ -244,7 +258,7 @@ setRemoting(DataModel.count, {
*/ */
DataModel.prototype.save = function (options, callback) { 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 () { DataModel.prototype.isNewRecord = function () {
throwNotAttached(this.constructor.modelName, 'isNewRecord');
}; };
/** /**
@ -266,7 +280,7 @@ DataModel.prototype.isNewRecord = function () {
DataModel.prototype.remove = DataModel.prototype.remove =
DataModel.prototype.delete = DataModel.prototype.delete =
DataModel.prototype.destroy = function (cb) { 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) { 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) { DataModel.prototype.updateAttributes = function updateAttributes(data, cb) {
throwNotAttached(this.modelName, 'updateAttributes');
}; };
// updateAttributes ~ remoting attributes // updateAttributes ~ remoting attributes
@ -313,5 +327,5 @@ setRemoting(DataModel.prototype.updateAttributes, {
*/ */
DataModel.prototype.reload = function reload(callback) { DataModel.prototype.reload = function reload(callback) {
throwNotAttached(this.constructor.modelName, 'reload');
}; };