Skip to content

Commit c94866f

Browse files
committed
Changes many errors to use the ErrorCodes generator (#206)
1 parent 9f2cde7 commit c94866f

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

lib/Associations/Many.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
var InstanceConstructor = require("../Instance").Instance;
22
var Settings = require("../Settings");
33
var Property = require("../Property");
4+
var ErrorCodes = require("../ErrorCodes");
45

56
exports.prepare = function (Model, associations) {
67
if (Model.keys.length > 1) {
78
Model.hasMany = function () {
8-
throw new Error("Model.hasMany() does not support multiple keys models");
9+
throw ErrorCodes.generateError(ErrorCodes.NO_SUPPORT, "Model.hasMany() does not support multiple keys models");
910
};
1011
return;
1112
}
@@ -209,7 +210,7 @@ function extendInstance(Model, Instance, Driver, association, opts, cb) {
209210
typeof Instances[Instances.length - 1] == "function" ? Instances.pop() : noOperation);
210211

211212
if (Instances.length === 0) {
212-
throw new Error("No associations defined");
213+
throw ErrorCodes.generateError(ErrorCodes.PARAM_MISSMATCH, "No associations defined");
213214
}
214215

215216
if (Array.isArray(Instances[0])) {

lib/Associations/One.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ exports.prepare = function (Model, associations, association_properties, model_f
5656
}
5757

5858
if (conditions === null) {
59-
throw new Error(".findBy" + assocName + "() is missing a conditions object");
59+
throw ErrorCodes.generateError(ErrorCodes.PARAM_MISSMATCH, ".findBy(" + assocName + ") is missing a conditions object");
6060
}
6161

6262
options.__merge = {

lib/Model.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var Property = require("./Property");
77
var Singleton = require("./Singleton");
88
var Utilities = require("./Utilities");
99
var Validators = require("./Validators");
10+
var ErrorCodes = require("./ErrorCodes");
1011

1112
exports.Model = Model;
1213

@@ -161,7 +162,7 @@ function Model(opts) {
161162
return this;
162163
}
163164

164-
return cb(new Error("Driver does not support Model.drop()"));
165+
return cb(ErrorCodes.generateError(ErrorCodes.NO_SUPPORT, "Driver does not support Model.drop()"));
165166
};
166167

167168
model.sync = function (cb) {
@@ -181,7 +182,7 @@ function Model(opts) {
181182
return this;
182183
}
183184

184-
return cb(new Error("Driver does not support Model.sync()"));
185+
return cb(ErrorCodes.generateError(ErrorCodes.NO_SUPPORT, "Driver does not support Model.sync()"));
185186
};
186187

187188
model.get = function () {
@@ -191,7 +192,7 @@ function Model(opts) {
191192
var cb = ids.pop();
192193

193194
if (typeof cb != "function") {
194-
throw new Error("Missing Model.get() callback");
195+
throw ErrorCodes.generateError(ErrorCodes.MISSING_CALLBACK, "Missing Model.get() callback");
195196
}
196197

197198
if (typeof ids[ids.length - 1] == "object" && !Array.isArray(ids[ids.length - 1])) {
@@ -203,7 +204,7 @@ function Model(opts) {
203204
}
204205

205206
if (ids.length != opts.keys.length) {
206-
throw new Error("Model.get() IDs number missmatch (" + opts.keys.length + " needed, " + ids.length + " passed)");
207+
throw ErrorCodes.generateError(ErrorCodes.PARAM_MISSMATCH, "Model.get() IDs number missmatch (" + opts.keys.length + " needed, " + ids.length + " passed)");
207208
}
208209

209210
for (var i = 0; i < opts.keys.length; i++) {
@@ -219,10 +220,10 @@ function Model(opts) {
219220

220221
opts.driver.find(model_fields, opts.table, conditions, { limit: 1 }, function (err, data) {
221222
if (err) {
222-
return cb(err);
223+
return cb(ErrorCodes.generateError(ErrorCodes.QUERY_ERROR, err.message, { originalCode: err.code }));
223224
}
224225
if (data.length === 0) {
225-
return cb(new Error("Not found"));
226+
return cb(ErrorCodes.generateError(ErrorCodes.NOT_FOUND, "Not found"));
226227
}
227228
Singleton.get(opts.driver.uid + "/" + opts.table + "/" + ids.join("/"), {
228229
cache : (options.hasOwnProperty("cache") ? options.cache : opts.cache),
@@ -366,7 +367,7 @@ function Model(opts) {
366367
}
367368

368369
if (cb === null) {
369-
throw new Error("Model.one() called without callback");
370+
throw ErrorCodes.generateError(ErrorCodes.MISSING_CALLBACK, "Missing Model.one() callback");
370371
}
371372

372373
// add limit 1
@@ -397,7 +398,7 @@ function Model(opts) {
397398
}
398399

399400
if (typeof cb != "function") {
400-
throw new Error("Missing Model.count() callback");
401+
throw ErrorCodes.generateError(ErrorCodes.MISSING_CALLBACK, "Missing Model.count() callback");
401402
}
402403

403404
opts.driver.count(opts.table, conditions, {}, function (err, data) {
@@ -437,7 +438,7 @@ function Model(opts) {
437438
var cb = ids.pop();
438439

439440
if (typeof cb != "function") {
440-
throw new Error("Missing Model.exists() callback");
441+
throw ErrorCodes.generateError(ErrorCodes.MISSING_CALLBACK, "Missing Model.exists() callback");
441442
}
442443

443444
var conditions = {}, i;

0 commit comments

Comments
 (0)