@@ -45,49 +45,48 @@ function create(resourceName, attrs, options) {
4545 var deferred = this . $q . defer ( ) ;
4646 var promise = deferred . promise ;
4747
48- options = options || { } ;
48+ try {
49+ options = options || { } ;
4950
50- if ( ! this . definitions [ resourceName ] ) {
51- deferred . reject ( new this . errors . NER ( errorPrefix + resourceName ) ) ;
52- } else if ( ! this . utils . isObject ( attrs ) ) {
53- deferred . reject ( new this . errors . IA ( errorPrefix + 'attrs: Must be an object!' ) ) ;
54- } else {
55- try {
56- var definition = this . definitions [ resourceName ] ;
57- var resource = this . store [ resourceName ] ;
58- var _this = this ;
51+ if ( ! this . definitions [ resourceName ] ) {
52+ throw new this . errors . NER ( errorPrefix + resourceName ) ;
53+ } else if ( ! this . utils . isObject ( attrs ) ) {
54+ throw new this . errors . IA ( errorPrefix + 'attrs: Must be an object!' ) ;
55+ }
56+ var definition = this . definitions [ resourceName ] ;
57+ var resource = this . store [ resourceName ] ;
58+ var _this = this ;
5959
60- promise = promise
61- . then ( function ( attrs ) {
62- return _this . $q . promisify ( definition . beforeValidate ) ( resourceName , attrs ) ;
63- } )
64- . then ( function ( attrs ) {
65- return _this . $q . promisify ( definition . validate ) ( resourceName , attrs ) ;
66- } )
67- . then ( function ( attrs ) {
68- return _this . $q . promisify ( definition . afterValidate ) ( resourceName , attrs ) ;
69- } )
70- . then ( function ( attrs ) {
71- return _this . $q . promisify ( definition . beforeCreate ) ( resourceName , attrs ) ;
72- } )
73- . then ( function ( attrs ) {
74- return _this . adapters [ options . adapter || definition . defaultAdapter ] . create ( definition , definition . serialize ( resourceName , attrs ) , options ) ;
75- } )
76- . then ( function ( res ) {
77- return _this . $q . promisify ( definition . afterCreate ) ( resourceName , definition . deserialize ( resourceName , res ) ) ;
78- } )
79- . then ( function ( data ) {
80- var created = _this . inject ( definition . name , data ) ;
81- var id = created [ definition . idAttribute ] ;
82- resource . previousAttributes [ id ] = _this . utils . deepMixIn ( { } , created ) ;
83- resource . saved [ id ] = _this . utils . updateTimestamp ( resource . saved [ id ] ) ;
84- return _this . get ( definition . name , id ) ;
85- } ) ;
60+ promise = promise
61+ . then ( function ( attrs ) {
62+ return _this . $q . promisify ( definition . beforeValidate ) ( resourceName , attrs ) ;
63+ } )
64+ . then ( function ( attrs ) {
65+ return _this . $q . promisify ( definition . validate ) ( resourceName , attrs ) ;
66+ } )
67+ . then ( function ( attrs ) {
68+ return _this . $q . promisify ( definition . afterValidate ) ( resourceName , attrs ) ;
69+ } )
70+ . then ( function ( attrs ) {
71+ return _this . $q . promisify ( definition . beforeCreate ) ( resourceName , attrs ) ;
72+ } )
73+ . then ( function ( attrs ) {
74+ return _this . adapters [ options . adapter || definition . defaultAdapter ] . create ( definition , definition . serialize ( resourceName , attrs ) , options ) ;
75+ } )
76+ . then ( function ( res ) {
77+ return _this . $q . promisify ( definition . afterCreate ) ( resourceName , definition . deserialize ( resourceName , res ) ) ;
78+ } )
79+ . then ( function ( data ) {
80+ var created = _this . inject ( definition . name , data ) ;
81+ var id = created [ definition . idAttribute ] ;
82+ resource . previousAttributes [ id ] = _this . utils . deepMixIn ( { } , created ) ;
83+ resource . saved [ id ] = _this . utils . updateTimestamp ( resource . saved [ id ] ) ;
84+ return _this . get ( definition . name , id ) ;
85+ } ) ;
8686
87- deferred . resolve ( attrs ) ;
88- } catch ( err ) {
89- deferred . reject ( err ) ;
90- }
87+ deferred . resolve ( attrs ) ;
88+ } catch ( err ) {
89+ deferred . reject ( err ) ;
9190 }
9291
9392 return promise ;
0 commit comments