Skip to content

Commit 600ef85

Browse files
committed
Further simplified error handling. #82.
1 parent 63e54ec commit 600ef85

18 files changed

+594
-508
lines changed

dist/angular-data.js

+298-255
Large diffs are not rendered by default.

dist/angular-data.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datastore/async_methods/create.js

+39-40
Original file line numberDiff line numberDiff line change
@@ -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;

src/datastore/async_methods/destroy.js

+30-30
Original file line numberDiff line numberDiff line change
@@ -46,40 +46,40 @@ function destroy(resourceName, id, options) {
4646
var deferred = this.$q.defer();
4747
var promise = deferred.promise;
4848

49-
options = options || {};
49+
try {
50+
options = options || {};
51+
52+
if (!this.definitions[resourceName]) {
53+
throw new this.errors.NER(errorPrefix + resourceName);
54+
} else if (!this.utils.isString(id) && !this.utils.isNumber(id)) {
55+
throw new this.errors.IA(errorPrefix + 'id: Must be a string or a number!');
56+
}
5057

51-
if (!this.definitions[resourceName]) {
52-
deferred.reject(new this.errors.NER(errorPrefix + resourceName));
53-
} else if (!this.utils.isString(id) && !this.utils.isNumber(id)) {
54-
deferred.reject(new this.errors.IA(errorPrefix + 'id: Must be a string or a number!'));
55-
} else {
5658
var item = this.get(resourceName, id);
5759
if (!item) {
58-
deferred.reject(new this.errors.R(errorPrefix + 'id: "' + id + '" not found!'));
59-
} else {
60-
try {
61-
var definition = this.definitions[resourceName];
62-
var _this = this;
63-
64-
promise = promise
65-
.then(function (attrs) {
66-
return _this.$q.promisify(definition.beforeDestroy)(resourceName, attrs);
67-
})
68-
.then(function () {
69-
return _this.adapters[options.adapter || definition.defaultAdapter].destroy(definition, id, options);
70-
})
71-
.then(function () {
72-
return _this.$q.promisify(definition.afterDestroy)(resourceName, item);
73-
})
74-
.then(function () {
75-
_this.eject(resourceName, id);
76-
return id;
77-
});
78-
deferred.resolve(item);
79-
} catch (err) {
80-
deferred.reject(err);
81-
}
60+
throw new this.errors.R(errorPrefix + 'id: "' + id + '" not found!');
8261
}
62+
63+
var definition = this.definitions[resourceName];
64+
var _this = this;
65+
66+
promise = promise
67+
.then(function (attrs) {
68+
return _this.$q.promisify(definition.beforeDestroy)(resourceName, attrs);
69+
})
70+
.then(function () {
71+
return _this.adapters[options.adapter || definition.defaultAdapter].destroy(definition, id, options);
72+
})
73+
.then(function () {
74+
return _this.$q.promisify(definition.afterDestroy)(resourceName, item);
75+
})
76+
.then(function () {
77+
_this.eject(resourceName, id);
78+
return id;
79+
});
80+
deferred.resolve(item);
81+
} catch (err) {
82+
deferred.reject(err);
8383
}
8484

8585
return promise;

src/datastore/async_methods/destroyAll.js

+23-21
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,33 @@ var errorPrefix = 'DS.destroyAll(resourceName, params[, options]): ';
5555
function destroyAll(resourceName, params, options) {
5656
var deferred = this.$q.defer();
5757
var promise = deferred.promise;
58-
var _this = this;
5958

60-
options = options || {};
59+
try {
60+
var _this = this;
61+
var IA = this.errors.IA;
6162

62-
if (!this.definitions[resourceName]) {
63-
deferred.reject(new this.errors.NER(errorPrefix + resourceName));
64-
} else if (!this.utils.isObject(params)) {
65-
deferred.reject(new this.errors.IA(errorPrefix + 'params: Must be an object!'));
66-
} else if (!this.utils.isObject(options)) {
67-
deferred.reject(new this.errors.IA(errorPrefix + 'options: Must be an object!'));
68-
} else {
69-
try {
70-
var definition = this.definitions[resourceName];
63+
options = options || {};
7164

72-
promise = promise
73-
.then(function () {
74-
return _this.adapters[options.adapter || definition.defaultAdapter].destroyAll(definition, params, options);
75-
})
76-
.then(function () {
77-
return _this.ejectAll(resourceName, params);
78-
});
79-
deferred.resolve();
80-
} catch (err) {
81-
deferred.reject(err);
65+
if (!this.definitions[resourceName]) {
66+
throw new this.errors.NER(errorPrefix + resourceName);
67+
} else if (!this.utils.isObject(params)) {
68+
throw new IA(errorPrefix + 'params: Must be an object!');
69+
} else if (!this.utils.isObject(options)) {
70+
throw new IA(errorPrefix + 'options: Must be an object!');
8271
}
72+
73+
var definition = this.definitions[resourceName];
74+
75+
promise = promise
76+
.then(function () {
77+
return _this.adapters[options.adapter || definition.defaultAdapter].destroyAll(definition, params, options);
78+
})
79+
.then(function () {
80+
return _this.ejectAll(resourceName, params);
81+
});
82+
deferred.resolve();
83+
} catch (err) {
84+
deferred.reject(err);
8385
}
8486

8587
return promise;

src/datastore/async_methods/find.js

+41-38
Original file line numberDiff line numberDiff line change
@@ -47,55 +47,58 @@ function find(resourceName, id, options) {
4747
var deferred = this.$q.defer();
4848
var promise = deferred.promise;
4949

50-
options = options || {};
50+
try {
51+
var IA = this.errors.IA;
52+
53+
options = options || {};
54+
55+
if (!this.definitions[resourceName]) {
56+
throw new this.errors.NER(errorPrefix + resourceName);
57+
} else if (!this.utils.isString(id) && !this.utils.isNumber(id)) {
58+
throw new IA(errorPrefix + 'id: Must be a string or a number!');
59+
} else if (!this.utils.isObject(options)) {
60+
throw new IA(errorPrefix + 'options: Must be an object!');
61+
}
5162

52-
if (!this.definitions[resourceName]) {
53-
deferred.reject(new this.errors.NER(errorPrefix + resourceName));
54-
} else if (!this.utils.isString(id) && !this.utils.isNumber(id)) {
55-
deferred.reject(new this.errors.IA(errorPrefix + 'id: Must be a string or a number!'));
56-
} else if (!this.utils.isObject(options)) {
57-
deferred.reject(new this.errors.IA(errorPrefix + 'options: Must be an object!'));
58-
} else {
5963
if (!('cacheResponse' in options)) {
6064
options.cacheResponse = true;
6165
} else {
6266
options.cacheResponse = !!options.cacheResponse;
6367
}
64-
try {
65-
var definition = this.definitions[resourceName];
66-
var resource = this.store[resourceName];
67-
var _this = this;
6868

69-
if (options.bypassCache) {
70-
delete resource.completedQueries[id];
71-
}
69+
var definition = this.definitions[resourceName];
70+
var resource = this.store[resourceName];
71+
var _this = this;
7272

73-
if (!(id in resource.completedQueries)) {
74-
if (!(id in resource.pendingQueries)) {
75-
promise = resource.pendingQueries[id] = _this.adapters[options.adapter || definition.defaultAdapter].find(definition, id, options)
76-
.then(function (res) {
77-
var data = definition.deserialize(resourceName, res);
78-
if (options.cacheResponse) {
79-
// Query is no longer pending
80-
delete resource.pendingQueries[id];
81-
resource.completedQueries[id] = new Date().getTime();
82-
return _this.inject(resourceName, data);
83-
} else {
84-
return data;
85-
}
86-
}, function (err) {
87-
delete resource.pendingQueries[id];
88-
return _this.$q.reject(err);
89-
});
90-
}
73+
if (options.bypassCache) {
74+
delete resource.completedQueries[id];
75+
}
9176

92-
return resource.pendingQueries[id];
93-
} else {
94-
deferred.resolve(_this.get(resourceName, id));
77+
if (!(id in resource.completedQueries)) {
78+
if (!(id in resource.pendingQueries)) {
79+
promise = resource.pendingQueries[id] = _this.adapters[options.adapter || definition.defaultAdapter].find(definition, id, options)
80+
.then(function (res) {
81+
var data = definition.deserialize(resourceName, res);
82+
if (options.cacheResponse) {
83+
// Query is no longer pending
84+
delete resource.pendingQueries[id];
85+
resource.completedQueries[id] = new Date().getTime();
86+
return _this.inject(resourceName, data);
87+
} else {
88+
return data;
89+
}
90+
}, function (err) {
91+
delete resource.pendingQueries[id];
92+
return _this.$q.reject(err);
93+
});
9594
}
96-
} catch (err) {
97-
deferred.reject(err);
95+
96+
return resource.pendingQueries[id];
97+
} else {
98+
deferred.resolve(_this.get(resourceName, id));
9899
}
100+
} catch (err) {
101+
deferred.reject(err);
99102
}
100103

101104
return promise;

src/datastore/async_methods/findAll.js

+21-18
Original file line numberDiff line numberDiff line change
@@ -120,31 +120,34 @@ function _findAll(utils, resourceName, params, options) {
120120
function findAll(resourceName, params, options) {
121121
var deferred = this.$q.defer();
122122
var promise = deferred.promise;
123-
var _this = this;
124123

125-
options = options || {};
126-
params = params || {};
124+
try {
125+
var IA = this.errors.IA;
126+
var _this = this;
127+
128+
options = options || {};
129+
params = params || {};
130+
131+
if (!this.definitions[resourceName]) {
132+
throw new this.errors.NER(errorPrefix + resourceName);
133+
} else if (!this.utils.isObject(params)) {
134+
throw new IA(errorPrefix + 'params: Must be an object!');
135+
} else if (!this.utils.isObject(options)) {
136+
throw new IA(errorPrefix + 'options: Must be an object!');
137+
}
127138

128-
if (!this.definitions[resourceName]) {
129-
deferred.reject(new this.errors.NER(errorPrefix + resourceName));
130-
} else if (!this.utils.isObject(params)) {
131-
deferred.reject(new this.errors.IA(errorPrefix + 'params: Must be an object!'));
132-
} else if (!this.utils.isObject(options)) {
133-
deferred.reject(new this.errors.IA(errorPrefix + 'options: Must be an object!'));
134-
} else {
135139
if (!('cacheResponse' in options)) {
136140
options.cacheResponse = true;
137141
} else {
138142
options.cacheResponse = !!options.cacheResponse;
139143
}
140-
try {
141-
promise = promise.then(function () {
142-
return _findAll.apply(_this, [_this.utils, resourceName, params, options]);
143-
});
144-
deferred.resolve();
145-
} catch (err) {
146-
deferred.reject(err);
147-
}
144+
145+
promise = promise.then(function () {
146+
return _findAll.apply(_this, [_this.utils, resourceName, params, options]);
147+
});
148+
deferred.resolve();
149+
} catch (err) {
150+
deferred.reject(err);
148151
}
149152

150153
return promise;

src/datastore/async_methods/loadRelations.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,20 @@ var errorPrefix = 'DS.loadRelations(resourceName, instance(Id), relations[, opti
3636
function loadRelations(resourceName, instance, relations, options) {
3737
var deferred = this.$q.defer();
3838
var promise = deferred.promise;
39-
var IA = this.errors.IA;
4039

41-
options = options || {};
40+
try {
41+
var IA = this.errors.IA;
4242

43-
if (angular.isString(instance) || angular.isNumber(instance)) {
44-
instance = this.get(resourceName, instance);
45-
}
43+
options = options || {};
4644

47-
if (angular.isString(relations)) {
48-
relations = [relations];
49-
}
45+
if (angular.isString(instance) || angular.isNumber(instance)) {
46+
instance = this.get(resourceName, instance);
47+
}
48+
49+
if (angular.isString(relations)) {
50+
relations = [relations];
51+
}
5052

51-
try {
5253
if (!this.definitions[resourceName]) {
5354
throw new this.errors.NER(errorPrefix + resourceName);
5455
} else if (!this.utils.isObject(instance)) {

0 commit comments

Comments
 (0)