Skip to content

Commit 3313e36

Browse files
committed
Changes more errors to use the new ErrorCodes generator (#206)
1 parent c94866f commit 3313e36

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

lib/AggregateFunctions.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ module.exports = AggregateFunctions;
44

55
function AggregateFunctions(opts) {
66
if (typeof opts.driver.getQuery != "function") {
7-
throw new Error("This driver does not support aggregate functions");
7+
throw ErrorCodes.generateError(ErrorCodes.NO_SUPPORT, "This driver does not support aggregate functions");
88
}
99
if (!Array.isArray(opts.driver.aggregate_functions)) {
10-
throw new Error("This driver does not support aggregate functions");
10+
throw ErrorCodes.generateError(ErrorCodes.NO_SUPPORT, "This driver does not support aggregate functions");
1111
}
1212

1313
var aggregates = [ [] ];
@@ -51,7 +51,7 @@ function AggregateFunctions(opts) {
5151
},
5252
select: function () {
5353
if (arguments.length === 0) {
54-
throw new Error("When using append you must at least define one property");
54+
throw ErrorCodes.generateError(ErrorCodes.PARAM_MISSMATCH, "When using append you must at least define one property");
5555
}
5656
opts.properties = opts.properties.concat(Array.isArray(arguments[0]) ?
5757
arguments[0] :
@@ -60,7 +60,7 @@ function AggregateFunctions(opts) {
6060
},
6161
as: function (alias) {
6262
if (aggregates.length === 0) {
63-
throw new Error("No aggregate function defined yet");
63+
throw ErrorCodes.generateError(ErrorCodes.PARAM_MISSMATCH, "No aggregate functions defined yet");
6464
}
6565

6666
var len = aggregates.length;
@@ -71,13 +71,13 @@ function AggregateFunctions(opts) {
7171
},
7272
get: function (cb) {
7373
if (typeof cb != "function") {
74-
throw new Error("You must pass a callback to Model.aggregate().get()");
74+
throw ErrorCodes.generateError(ErrorCodes.MISSING_CALLBACK, "You must pass a callback to Model.aggregate().get()");
7575
}
7676
if (aggregates[aggregates.length - 1].length === 0) {
7777
aggregates.length -= 1;
7878
}
7979
if (aggregates.length === 0) {
80-
throw new Error("Missing aggregate functions");
80+
throw ErrorCodes.generateError(ErrorCodes.PARAM_MISSMATCH, "Missing aggregate functions");
8181
}
8282

8383
var query = opts.driver.getQuery().select().from(opts.table).select(opts.properties);

lib/ErrorCodes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ exports.NO_SUPPORT = 3;
44
exports.MISSING_CALLBACK = 4;
55
exports.PARAM_MISSMATCH = 5;
66

7+
exports.CONNECTION_LOST = 10;
8+
79
Object.defineProperty(exports, "generateError", {
810
value: function (code, message, extra) {
911
var err = new Error(message);

lib/ORM.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ exports.use = function (connection, proto, opts, cb) {
5050

5151
exports.connect = function (opts, cb) {
5252
if (arguments.length === 0 || !opts) {
53-
return ORM_Error(new Error("CONNECTION_URL_EMPTY"), cb);
53+
return ORM_Error(ErrorCodes.generateError(ErrorCodes.PARAM_MISSMATCH, "CONNECTION_URL_EMPTY"), cb);
5454
}
5555
if (typeof opts == "string") {
5656
if (opts.replace(/\s+/, "").length === 0) {
57-
return ORM_Error(new Error("CONNECTION_URL_EMPTY"), cb);
57+
return ORM_Error(ErrorCodes.generateError(ErrorCodes.PARAM_MISSMATCH, "CONNECTION_URL_EMPTY"), cb);
5858
}
5959
opts = url.parse(opts, true);
6060
}
@@ -65,7 +65,7 @@ exports.connect = function (opts, cb) {
6565
opts.database = (opts.pathname ? opts.pathname.substr(1) : "");
6666
}
6767
if (!opts.protocol) {
68-
return ORM_Error(new Error("CONNECTION_URL_NO_PROTOCOL"), cb);
68+
return ORM_Error(ErrorCodes.generateError(ErrorCodes.PARAM_MISSMATCH, "CONNECTION_URL_NO_PROTOCOL"), cb);
6969
}
7070
// if (!opts.host) {
7171
// opts.host = opts.hostname = "localhost";
@@ -111,7 +111,7 @@ exports.connect = function (opts, cb) {
111111
});
112112
} catch (ex) {
113113
if (ex.code == "MODULE_NOT_FOUND" || ex.message.indexOf('find module')) {
114-
return ORM_Error(new Error("CONNECTION_PROTOCOL_NOT_SUPPORTED"), cb);
114+
return ORM_Error(ErrorCodes.generateError(ErrorCodes.NO_SUPPORT, "CONNECTION_PROTOCOL_NOT_SUPPORTED"), cb);
115115
}
116116
return ORM_Error(ex, cb);
117117
}
@@ -138,7 +138,7 @@ function ORM(driver_name, driver, settings) {
138138
var onError = function (err) {
139139
if (this.settings.get("connection.reconnect")) {
140140
if (typeof this.driver.reconnect == "undefined") {
141-
return this.emit("error", new Error("Connection lost - driver does not support reconnection"));
141+
return this.emit("error", ErrorCodes.generateError(ErrorCodes.CONNECTION_LOST, "Connection lost - driver does not support reconnection"));
142142
}
143143
this.driver.reconnect(function () {
144144
this.driver.on("error", onError);

lib/Property.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ exports.normalize = function (prop, Settings) {
3030

3131
if ([ "text", "number", "boolean", "date", "enum", "object", "binary" ].indexOf(prop.type) == -1) {
3232
throw new Error("Unknown property type: " + prop.type);
33+
throw ErrorCodes.generateError(ErrorCodes.NO_SUPPORT, "Unknown property type: " + prop.type);
3334
}
3435

3536
if (!prop.hasOwnProperty("required") && Settings.get("properties.required")) {

0 commit comments

Comments
 (0)