Skip to content

Commit bfdfc7e

Browse files
committed
feat($q): report promises with non rejection callback
Rejected promises that do not have a callback to handle the rejection report this to $exceptionHandler so they can be logged to the console. BREAKING CHANGE Tests that depend on specific order or number of messages in $exceptionHandler will need to handle rejected promises report. Closes: angular#13653 Closes: angular#7992
1 parent c9b4251 commit bfdfc7e

File tree

12 files changed

+184
-43
lines changed

12 files changed

+184
-43
lines changed

src/ng/compile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2716,7 +2716,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
27162716
childBoundTranscludeFn);
27172717
}
27182718
linkQueue = null;
2719-
});
2719+
}).then(noop, noop);
27202720

27212721
return function delayedNodeLinkFn(ignoreChildLinkFn, scope, node, rootElement, boundTranscludeFn) {
27222722
var childBoundTranscludeFn = boundTranscludeFn;

src/ng/interval.js

+2
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ function $IntervalProvider() {
188188
*/
189189
interval.cancel = function(promise) {
190190
if (promise && promise.$$intervalId in intervals) {
191+
// Interval cancels should not report as unhandled promise.
192+
intervals[promise.$$intervalId].promise.then(noop, noop);
191193
intervals[promise.$$intervalId].reject('canceled');
192194
$window.clearInterval(promise.$$intervalId);
193195
delete intervals[promise.$$intervalId];

src/ng/q.js

+87-18
Original file line numberDiff line numberDiff line change
@@ -217,20 +217,51 @@
217217
* @returns {Promise} The newly created promise.
218218
*/
219219
function $QProvider() {
220-
220+
var errorOnUnhandledRejections = true;
221221
this.$get = ['$rootScope', '$exceptionHandler', function($rootScope, $exceptionHandler) {
222222
return qFactory(function(callback) {
223223
$rootScope.$evalAsync(callback);
224-
}, $exceptionHandler);
224+
}, $exceptionHandler, errorOnUnhandledRejections);
225225
}];
226+
227+
/**
228+
* @ngdoc method
229+
* @name $qProvider#errorOnUnhandledRejections
230+
* @kind function
231+
*
232+
* @description
233+
* Retrieves or overrides on whether to generate an error when a rejected promise is not handled.
234+
*
235+
* @param {boolean=} value Whether to generate an error when a rejected promise is not handled.
236+
* @returns {boolean|ng.$qProvider} Current value when called without a new value or self for
237+
* chaining otherwise.
238+
*/
239+
this.errorOnUnhandledRejections = function(value) {
240+
if (isDefined(value)) {
241+
errorOnUnhandledRejections = value;
242+
return this;
243+
} else {
244+
return errorOnUnhandledRejections;
245+
}
246+
};
226247
}
227248

228249
function $$QProvider() {
250+
var errorOnUnhandledRejections = true;
229251
this.$get = ['$browser', '$exceptionHandler', function($browser, $exceptionHandler) {
230252
return qFactory(function(callback) {
231253
$browser.defer(callback);
232-
}, $exceptionHandler);
254+
}, $exceptionHandler, errorOnUnhandledRejections);
233255
}];
256+
257+
this.errorOnUnhandledRejections = function(value) {
258+
if (isDefined(value)) {
259+
errorOnUnhandledRejections = value;
260+
return this;
261+
} else {
262+
return errorOnUnhandledRejections;
263+
}
264+
};
234265
}
235266

236267
/**
@@ -239,10 +270,14 @@ function $$QProvider() {
239270
* @param {function(function)} nextTick Function for executing functions in the next turn.
240271
* @param {function(...*)} exceptionHandler Function into which unexpected exceptions are passed for
241272
* debugging purposes.
273+
@ param {=boolean} errorOnUnhandledRejections Whether an error should be generated on unhandled
274+
* promises rejections.
242275
* @returns {object} Promise manager.
243276
*/
244-
function qFactory(nextTick, exceptionHandler) {
277+
function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) {
245278
var $qMinErr = minErr('$q', TypeError);
279+
var queueSize = 0;
280+
var checkQueue = [];
246281

247282
/**
248283
* @ngdoc method
@@ -307,28 +342,62 @@ function qFactory(nextTick, exceptionHandler) {
307342
pending = state.pending;
308343
state.processScheduled = false;
309344
state.pending = undefined;
310-
for (var i = 0, ii = pending.length; i < ii; ++i) {
311-
deferred = pending[i][0];
312-
fn = pending[i][state.status];
313-
try {
314-
if (isFunction(fn)) {
315-
deferred.resolve(fn(state.value));
316-
} else if (state.status === 1) {
317-
deferred.resolve(state.value);
318-
} else {
319-
deferred.reject(state.value);
345+
try {
346+
for (var i = 0, ii = pending.length; i < ii; ++i) {
347+
state.pur = true;
348+
deferred = pending[i][0];
349+
fn = pending[i][state.status];
350+
try {
351+
if (isFunction(fn)) {
352+
deferred.resolve(fn(state.value));
353+
} else if (state.status === 1) {
354+
deferred.resolve(state.value);
355+
} else {
356+
deferred.reject(state.value);
357+
}
358+
} catch (e) {
359+
deferred.reject(e);
360+
exceptionHandler(e);
320361
}
321-
} catch (e) {
322-
deferred.reject(e);
323-
exceptionHandler(e);
362+
}
363+
} finally {
364+
--queueSize;
365+
if (errorOnUnhandledRejections && queueSize === 0) {
366+
nextTick(processChecksFn());
367+
}
368+
}
369+
}
370+
371+
function processChecks() {
372+
while (!queueSize && checkQueue.length) {
373+
var toCheck = checkQueue.shift();
374+
if (!toCheck.pur) {
375+
toCheck.pur = true;
376+
var errorMessage = 'Possibly unhandled rejection: ' + toDebugString(toCheck.value);
377+
exceptionHandler(errorMessage);
324378
}
325379
}
326380
}
327381

382+
function processChecksFn() {
383+
return function() { processChecks(); };
384+
}
385+
386+
function processQueueFn(state) {
387+
return function() { processQueue(state); };
388+
}
389+
328390
function scheduleProcessQueue(state) {
391+
if (errorOnUnhandledRejections && !state.pending && state.status === 2 && !state.pur) {
392+
if (queueSize === 0 && checkQueue.length === 0) {
393+
nextTick(processChecksFn());
394+
}
395+
checkQueue.push(state);
396+
}
329397
if (state.processScheduled || !state.pending) return;
330398
state.processScheduled = true;
331-
nextTick(function() { processQueue(state); });
399+
++queueSize;
400+
nextTick(processQueueFn(state));
332401
}
333402

334403
function Deferred() {

src/ng/timeout.js

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ function $TimeoutProvider() {
8585
*/
8686
timeout.cancel = function(promise) {
8787
if (promise && promise.$$timeoutId in deferreds) {
88+
// Timeout cancels should not report an unhandled promise.
89+
deferreds[promise.$$timeoutId].promise.then(noop, noop);
8890
deferreds[promise.$$timeoutId].reject('canceled');
8991
delete deferreds[promise.$$timeoutId];
9092
return $browser.defer.cancel(promise.$$timeoutId);

src/ngMock/angular-mocks.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ angular.mock.$IntervalProvider = function() {
445445
promise = deferred.promise;
446446

447447
count = (angular.isDefined(count)) ? count : 0;
448-
promise.then(null, null, (!hasParams) ? fn : function() {
448+
promise.then(null, function() {}, (!hasParams) ? fn : function() {
449449
fn.apply(null, args);
450450
});
451451

@@ -505,6 +505,7 @@ angular.mock.$IntervalProvider = function() {
505505
});
506506

507507
if (angular.isDefined(fnIndex)) {
508+
repeatFns[fnIndex].deferred.promise.then(undefined, function() {});
508509
repeatFns[fnIndex].deferred.reject('canceled');
509510
repeatFns.splice(fnIndex, 1);
510511
return true;

src/ngResource/resource.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -706,13 +706,14 @@ angular.module('ngResource', ['ng']).
706706
return $q.reject(response);
707707
});
708708

709-
promise['finally'](function() {
709+
promise = promise['finally'](function(response) {
710710
value.$resolved = true;
711711
if (!isInstanceCall && cancellable) {
712712
value.$cancelRequest = angular.noop;
713713
$timeout.cancel(numericTimeoutPromise);
714714
timeoutDeferred = numericTimeoutPromise = httpConfig.timeout = null;
715715
}
716+
return response;
716717
});
717718

718719
promise = promise.then(

test/ng/animateRunnerSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ describe("$$AnimateRunner", function() {
206206
var animationComplete = false;
207207
runner.finally(function() {
208208
animationComplete = true;
209-
});
209+
}).then(noop, noop);
210210
runner[method]();
211211
$rootScope.$digest();
212212
expect(animationComplete).toBe(true);

test/ng/httpSpec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ describe('$http', function() {
10311031

10321032
it('should $apply after error callback', function() {
10331033
$httpBackend.when('GET').respond(404);
1034-
$http({method: 'GET', url: '/some'});
1034+
$http({method: 'GET', url: '/some'}).catch(noop);
10351035
$httpBackend.flush();
10361036
expect($rootScope.$apply).toHaveBeenCalledOnce();
10371037
});
@@ -1432,7 +1432,7 @@ describe('$http', function() {
14321432

14331433
function doFirstCacheRequest(method, respStatus, headers) {
14341434
$httpBackend.expect(method || 'GET', '/url').respond(respStatus || 200, 'content', headers);
1435-
$http({method: method || 'GET', url: '/url', cache: cache});
1435+
$http({method: method || 'GET', url: '/url', cache: cache}).catch(noop);
14361436
$httpBackend.flush();
14371437
}
14381438

@@ -1640,7 +1640,7 @@ describe('$http', function() {
16401640

16411641
it('should preserve config object when rejecting from pending cache', function() {
16421642
$httpBackend.expect('GET', '/url').respond(404, 'content');
1643-
$http({method: 'GET', url: '/url', cache: cache, headers: {foo: 'bar'}});
1643+
$http({method: 'GET', url: '/url', cache: cache, headers: {foo: 'bar'}}).catch(noop);
16441644

16451645
$http({method: 'GET', url: '/url', cache: cache, headers: {foo: 'baz'}}).catch(callback);
16461646
$httpBackend.flush();

0 commit comments

Comments
 (0)