@@ -7,6 +7,7 @@ var Property = require("./Property");
77var Singleton = require ( "./Singleton" ) ;
88var Utilities = require ( "./Utilities" ) ;
99var Validators = require ( "./Validators" ) ;
10+ var ErrorCodes = require ( "./ErrorCodes" ) ;
1011
1112exports . 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