Permalink
Browse files

Append "catch" to handle fails - fix #103

  • Loading branch information...
1 parent d6479ab commit e1b2aa0d5f35237e1b8d3afc6d06fa2db28764ce @jbdemonte committed Dec 3, 2016
Showing with 78 additions and 5 deletions.
  1. +16 −4 src/gmap3.js
  2. +50 −0 tests/tests/chaining.js
  3. +12 −1 tests/tests/latlng.js
View
@@ -176,7 +176,7 @@
if (status === gm.GeocoderStatus.OK) {
dfd.resolve(results[0].geometry.location);
} else {
- dfd.reject();
+ dfd.reject(status);
}
});
return dfd;
@@ -263,13 +263,16 @@
if (address) {
delete options.address;
return geocode(address).then(function (latLng) {
- options[key] = latLng;
- });
+ options[key] = latLng;
+ });
}
options[key] = toLatLng(options[key]);
})
.then(function () {
dfd.resolve(fn(options));
+ })
+ .fail(function (reason) {
+ dfd.reject(reason);
});
return dfd;
}
@@ -892,7 +895,7 @@
/**
* Decorator to chain promise result onto the main promise chain
* @param {Function} fn
- * @returns {Deferred}
+ * @returns {Function}
*/
function chainToPromise(fn) {
return function () {
@@ -1085,6 +1088,15 @@
}
};
+ self.catch = function (fn) {
+ if (isFunction(fn)) {
+ promise = promise
+ .then(null, function (reason) {
+ return when(fn.call(context(), reason));
+ });
+ }
+ };
+
foreachStr('on once', function (name, once) {
self[name] = function () {
var events = arguments[0];
@@ -1,4 +1,15 @@
describe('chaining', function () {
+
+ function rejected(reason) {
+ var dfd = jQuery.Deferred();
+ dfd.reject(reason);
+ return dfd;
+ }
+ function resolved(reason) {
+ var dfd = jQuery.Deferred();
+ dfd.resolve(reason);
+ return dfd;
+ }
beforeEach(function (done) {
var self = this;
@@ -100,4 +111,43 @@ describe('chaining', function () {
});
});
+ it('should chain catch', function (done) {
+ var results = [];
+ this.handler
+ .marker(function (map) {
+ expect(map).to.be.an.instanceof(google.maps.Map);
+ return rejected(1);
+ })
+ .then(function () {
+ done(new Error('catch expected'));
+ })
+ .catch(function (value) {
+ results.push(value);
+ return resolved(2);
+ })
+ .then(function (value) {
+ results.push(value);
+ return rejected(3);
+ })
+ .then(function () {
+ done(new Error('catch expected'));
+ })
+ .catch(function (value) {
+ results.push(value);
+ return rejected(4);
+ })
+ .then(function () {
+ done(new Error('catch expected'));
+ })
+ .catch(function (value) {
+ results.push(value);
+ return 5;
+ })
+ .then(function (value) {
+ results.push(value);
+ expect(results).to.eql([1,2,3,4,5]);
+ done();
+ });
+ });
+
});
View
@@ -17,6 +17,18 @@ describe('latlng', function () {
});
});
+ it('would handle latlng fail', function (done) {
+ this.handler
+ .latlng({address: '>>>>>'})
+ .then(function () {
+ done(new Error('error expected'));
+ })
+ .catch(function (err) {
+ expect(err).to.be.equal('ZERO_RESULTS');
+ done();
+ });
+ });
+
it('would convert the position as array', function (done) {
this.handler
.latlng({latlng: [10,20]})
@@ -39,5 +51,4 @@ describe('latlng', function () {
done();
});
});
-
});

0 comments on commit e1b2aa0

Please sign in to comment.