fix($q): treat thrown errors as regular rejections

Previously, errors thrown in a promise's `onFulfilled` or `onRejected` handlers were treated in a
slightly different manner than regular rejections:
They were passed to the `$exceptionHandler()` (in addition to being converted to rejections).

The reasoning for this behavior was that an uncaught error is different than a regular rejection, as
it can be caused by a programming error, for example. In practice, this turned out to be confusing
or undesirable for users, since neither native promises nor any other popular promise library
distinguishes thrown errors from regular rejections.
(Note: While this behavior does not go against the Promises/A+ spec, it is not prescribed either.)

This commit removes the distinction, by skipping the call to `$exceptionHandler()`, thus treating
thrown errors as regular rejections.

**Note:**
Unless explicitly turned off, possibly unhandled rejections will still be caught and passed to the
`$exceptionHandler()`, so errors thrown due to programming errors and not otherwise handled (with a
subsequent `onRejected` handler) will not go unnoticed.

Fixes #3174
Fixes #14745

Closes #15213

BREAKING CHANGE:

Previously, throwing an error from a promise's `onFulfilled` or `onRejection` handlers, would result
in passing the error to the `$exceptionHandler()` (in addition to rejecting the promise with the
error as reason).

Now, a thrown error is treated exactly the same as a regular rejection. This applies to all
services/controllers/filters etc that rely on `$q` (including built-in services, such as `$http` and
`$route`). For example, `$http`'s `transformRequest/Response` functions or a route's `redirectTo`
function as well as functions specified in a route's `resolve` object, will no longer result in a
call to `$exceptionHandler()` if they throw an error. Other than that, everything will continue to
behave in the same way; i.e. the promises will be rejected, route transition will be cancelled,
`$routeChangeError` events will be broadcasted etc.
This commit is contained in:
Georgios Kalpakas
2016-09-30 20:43:31 +03:00
parent 823295fee0
commit e13eeabd7e
9 changed files with 190 additions and 213 deletions
@@ -9,21 +9,21 @@
minErr,
extend
*/
/* eslint-disable no-unused-vars */
function isFunction(value) { return typeof value === 'function'; }
function isPromiseLike(obj) { return obj && isFunction(obj.then); }
function isObject(value) { return value !== null && typeof value === 'object'; }
function isUndefined(value) { return typeof value === 'undefined'; }
var isFunction = function isFunction(value) {return typeof value === 'function';};
var isPromiseLike = function isPromiseLike(obj) {return obj && isFunction(obj.then);};
var isObject = function isObject(value) {return value != null && typeof value === 'object';};
var isUndefined = function isUndefined(value) {return typeof value === 'undefined';};
var minErr = function minErr(module, constructor) {
function minErr(module, constructor) {
return function() {
var ErrorConstructor = constructor || Error;
throw new ErrorConstructor(module + arguments[0] + arguments[1]);
};
};
}
var extend = function extend(dst) {
function extend(dst) {
for (var i = 1, ii = arguments.length; i < ii; i++) {
var obj = arguments[i];
if (obj) {
@@ -35,18 +35,11 @@ var extend = function extend(dst) {
}
}
return dst;
};
}
/* eslint-enable */
var $q = qFactory(process.nextTick, function noopExceptionHandler() {});
exports.resolved = $q.resolve;
exports.rejected = $q.reject;
exports.deferred = function() {
var deferred = $q.defer();
return {
promise: deferred.promise,
resolve: deferred.resolve,
reject: deferred.reject
};
};
exports.deferred = $q.defer;
+4
View File
@@ -3131,6 +3131,10 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
childBoundTranscludeFn);
}
linkQueue = null;
}).catch(function(error) {
if (error instanceof Error) {
$exceptionHandler(error);
}
}).catch(noop);
return function delayedNodeLinkFn(ignoreChildLinkFn, scope, node, rootElement, boundTranscludeFn) {
+3 -6
View File
@@ -9,8 +9,8 @@
* A service that helps you run functions asynchronously, and use their return values (or exceptions)
* when they are done processing.
*
* This is an implementation of promises/deferred objects inspired by
* [Kris Kowal's Q](https://github.com/kriskowal/q).
* This is a [Promises/A+](https://promisesaplus.com/)-compliant implementation of promises/deferred
* objects inspired by [Kris Kowal's Q](https://github.com/kriskowal/q).
*
* $q can be used in two fashions --- one which is more similar to Kris Kowal's Q or jQuery's Deferred
* implementations, and the other which resembles ES6 (ES2015) promises to some degree.
@@ -366,7 +366,6 @@ function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) {
}
} catch (e) {
deferred.reject(e);
exceptionHandler(e);
}
}
} finally {
@@ -417,7 +416,6 @@ function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) {
} else {
this.$$resolve(val);
}
},
$$resolve: function(val) {
@@ -425,7 +423,7 @@ function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) {
var that = this;
var done = false;
try {
if ((isObject(val) || isFunction(val))) then = val && val.then;
if (isObject(val) || isFunction(val)) then = val.then;
if (isFunction(then)) {
this.promise.$$state.status = -1;
then.call(val, resolvePromise, rejectPromise, simpleBind(this, this.notify));
@@ -436,7 +434,6 @@ function qFactory(nextTick, exceptionHandler, errorOnUnhandledRejections) {
}
} catch (e) {
rejectPromise(e);
exceptionHandler(e);
}
function resolvePromise(val) {
+50 -44
View File
@@ -60,53 +60,59 @@ function $TemplateRequestProvider() {
*
* @property {number} totalPendingRequests total amount of pending template requests being downloaded.
*/
this.$get = ['$templateCache', '$http', '$q', '$sce', function($templateCache, $http, $q, $sce) {
this.$get = ['$exceptionHandler', '$templateCache', '$http', '$q', '$sce',
function($exceptionHandler, $templateCache, $http, $q, $sce) {
function handleRequestFn(tpl, ignoreRequestError) {
handleRequestFn.totalPendingRequests++;
function handleRequestFn(tpl, ignoreRequestError) {
handleRequestFn.totalPendingRequests++;
// We consider the template cache holds only trusted templates, so
// there's no need to go through whitelisting again for keys that already
// are included in there. This also makes Angular accept any script
// directive, no matter its name. However, we still need to unwrap trusted
// types.
if (!isString(tpl) || isUndefined($templateCache.get(tpl))) {
tpl = $sce.getTrustedResourceUrl(tpl);
}
var transformResponse = $http.defaults && $http.defaults.transformResponse;
if (isArray(transformResponse)) {
transformResponse = transformResponse.filter(function(transformer) {
return transformer !== defaultHttpResponseTransform;
});
} else if (transformResponse === defaultHttpResponseTransform) {
transformResponse = null;
}
return $http.get(tpl, extend({
cache: $templateCache,
transformResponse: transformResponse
}, httpOptions))
.finally(function() {
handleRequestFn.totalPendingRequests--;
})
.then(function(response) {
$templateCache.put(tpl, response.data);
return response.data;
}, handleError);
function handleError(resp) {
if (!ignoreRequestError) {
throw $templateRequestMinErr('tpload', 'Failed to load template: {0} (HTTP status: {1} {2})',
tpl, resp.status, resp.statusText);
// We consider the template cache holds only trusted templates, so
// there's no need to go through whitelisting again for keys that already
// are included in there. This also makes Angular accept any script
// directive, no matter its name. However, we still need to unwrap trusted
// types.
if (!isString(tpl) || isUndefined($templateCache.get(tpl))) {
tpl = $sce.getTrustedResourceUrl(tpl);
}
var transformResponse = $http.defaults && $http.defaults.transformResponse;
if (isArray(transformResponse)) {
transformResponse = transformResponse.filter(function(transformer) {
return transformer !== defaultHttpResponseTransform;
});
} else if (transformResponse === defaultHttpResponseTransform) {
transformResponse = null;
}
return $http.get(tpl, extend({
cache: $templateCache,
transformResponse: transformResponse
}, httpOptions))
.finally(function() {
handleRequestFn.totalPendingRequests--;
})
.then(function(response) {
$templateCache.put(tpl, response.data);
return response.data;
}, handleError);
function handleError(resp) {
if (!ignoreRequestError) {
resp = $templateRequestMinErr('tpload',
'Failed to load template: {0} (HTTP status: {1} {2})',
tpl, resp.status, resp.statusText);
$exceptionHandler(resp);
}
return $q.reject(resp);
}
return $q.reject(resp);
}
handleRequestFn.totalPendingRequests = 0;
return handleRequestFn;
}
handleRequestFn.totalPendingRequests = 0;
return handleRequestFn;
}];
];
}
+42 -34
View File
@@ -1854,17 +1854,18 @@ describe('$compile', function() {
));
it('should throw an error and clear element content if the template fails to load', inject(
function($compile, $httpBackend, $rootScope) {
$httpBackend.expect('GET', 'hello.html').respond(404, 'Not Found!');
element = $compile('<div><b class="hello">content</b></div>')($rootScope);
it('should throw an error and clear element content if the template fails to load',
inject(function($compile, $exceptionHandler, $httpBackend, $rootScope) {
$httpBackend.expect('GET', 'hello.html').respond(404, 'Not Found!');
element = $compile('<div><b class="hello">content</b></div>')($rootScope);
expect(function() {
$httpBackend.flush();
}).toThrowMinErr('$compile', 'tpload', 'Failed to load template: hello.html');
expect(sortedHtml(element)).toBe('<div><b class="hello"></b></div>');
}
));
$httpBackend.flush();
expect(sortedHtml(element)).toBe('<div><b class="hello"></b></div>');
expect($exceptionHandler.errors[0].message).toMatch(
/^\[\$compile:tpload] Failed to load template: hello\.html/);
})
);
it('should prevent multiple templates per element', function() {
@@ -1878,13 +1879,15 @@ describe('$compile', function() {
templateUrl: 'template.html'
}));
});
inject(function($compile, $httpBackend) {
inject(function($compile, $exceptionHandler, $httpBackend) {
$httpBackend.whenGET('template.html').respond('<p>template.html</p>');
expect(function() {
$compile('<div><div class="sync async"></div></div>');
$httpBackend.flush();
}).toThrowMinErr('$compile', 'multidir', 'Multiple directives [async, sync] asking for template on: ' +
'<div class="sync async">');
$compile('<div><div class="sync async"></div></div>');
$httpBackend.flush();
expect($exceptionHandler.errors[0].message).toMatch(new RegExp(
'^\\[\\$compile:multidir] Multiple directives \\[async, sync] asking for ' +
'template on: <div class="sync async">'));
});
});
@@ -2667,14 +2670,15 @@ describe('$compile', function() {
);
it('should not allow more than one isolate/new scope creation per element regardless of `templateUrl`',
inject(function($httpBackend) {
inject(function($exceptionHandler, $httpBackend) {
$httpBackend.expect('GET', 'tiscope.html').respond('<div>Hello, world !</div>');
expect(function() {
compile('<div class="tiscope-a; scope-b"></div>');
$httpBackend.flush();
}).toThrowMinErr('$compile', 'multidir', 'Multiple directives [scopeB, tiscopeA] ' +
'asking for new/isolated scope on: <div class="tiscope-a; scope-b ng-scope">');
compile('<div class="tiscope-a; scope-b"></div>');
$httpBackend.flush();
expect($exceptionHandler.errors[0].message).toMatch(new RegExp(
'^\\[\\$compile:multidir] Multiple directives \\[scopeB, tiscopeA] ' +
'asking for new/isolated scope on: <div class="tiscope-a; scope-b ng-scope">'));
})
);
@@ -8875,28 +8879,29 @@ describe('$compile', function() {
'<div class="foo" ng-transclude></div>' +
'</div>',
transclude: true
}));
$compileProvider.directive('noTransBar', valueFn({
templateUrl: 'noTransBar.html',
transclude: false
}));
});
inject(function($compile, $rootScope, $templateCache) {
inject(function($compile, $exceptionHandler, $rootScope, $templateCache) {
$templateCache.put('noTransBar.html',
'<div>' +
// This ng-transclude is invalid. It should throw an error.
'<div class="bar" ng-transclude></div>' +
'</div>');
expect(function() {
element = $compile('<div trans-foo>content</div>')($rootScope);
$rootScope.$apply();
}).toThrowMinErr('ngTransclude', 'orphan',
'Illegal use of ngTransclude directive in the template! No parent directive that requires a transclusion found. Element: <div class="bar" ng-transclude="">');
element = $compile('<div trans-foo>content</div>')($rootScope);
$rootScope.$digest();
expect($exceptionHandler.errors[0][1]).toBe('<div class="bar" ng-transclude="">');
expect($exceptionHandler.errors[0][0].message).toMatch(new RegExp(
'^\\[ngTransclude:orphan] Illegal use of ngTransclude directive in the ' +
'template! No parent directive that requires a transclusion found. Element: ' +
'<div class="bar" ng-transclude="">'));
});
});
@@ -9706,12 +9711,15 @@ describe('$compile', function() {
transclude: 'element'
}));
});
inject(function($compile, $httpBackend) {
inject(function($compile, $exceptionHandler, $httpBackend) {
$httpBackend.expectGET('template.html').respond('<p second>template.html</p>');
$compile('<div template first></div>');
expect(function() {
$httpBackend.flush();
}).toThrowMinErr('$compile', 'multidir', /Multiple directives \[first, second\] asking for transclusion on: <p .+/);
$httpBackend.flush();
expect($exceptionHandler.errors[0].message).toMatch(new RegExp(
'^\\[\\$compile:multidir] Multiple directives \\[first, second] asking for ' +
'transclusion on: <p '));
});
});
+4 -10
View File
@@ -1941,7 +1941,7 @@ describe('$http', function() {
it('should increment/decrement `outstandingRequestCount` on error in `transformRequest`',
inject(function($exceptionHandler) {
function() {
expect(incOutstandingRequestCountSpy).not.toHaveBeenCalled();
expect(completeOutstandingRequestSpy).not.toHaveBeenCalled();
@@ -1954,15 +1954,12 @@ describe('$http', function() {
expect(incOutstandingRequestCountSpy).toHaveBeenCalledOnce();
expect(completeOutstandingRequestSpy).toHaveBeenCalledOnce();
expect($exceptionHandler.errors).toEqual([jasmine.any(Error)]);
$exceptionHandler.errors = [];
})
}
);
it('should increment/decrement `outstandingRequestCount` on error in `transformResponse`',
inject(function($exceptionHandler) {
function() {
expect(incOutstandingRequestCountSpy).not.toHaveBeenCalled();
expect(completeOutstandingRequestSpy).not.toHaveBeenCalled();
@@ -1976,10 +1973,7 @@ describe('$http', function() {
expect(incOutstandingRequestCountSpy).toHaveBeenCalledOnce();
expect(completeOutstandingRequestSpy).toHaveBeenCalledOnce();
expect($exceptionHandler.errors).toEqual([jasmine.any(Error)]);
$exceptionHandler.errors = [];
})
}
);
});
+9 -54
View File
@@ -2077,13 +2077,13 @@ describe('q', function() {
describe('in then', function() {
it('should log exceptions thrown in a success callback and reject the derived promise',
it('should NOT log exceptions thrown in a success callback but reject the derived promise',
function() {
var success1 = success(1, 'oops', true);
promise.then(success1).then(success(2), error(2)).catch(noop);
syncResolve(deferred, 'done');
expect(logStr()).toBe('success1(done)->throw(oops); error2(oops)->reject(oops)');
expect(mockExceptionLogger.log).toEqual(['oops']);
expect(mockExceptionLogger.log).toEqual([]);
});
@@ -2095,12 +2095,13 @@ describe('q', function() {
});
it('should log exceptions thrown in a errback and reject the derived promise', function() {
it('should NOT log exceptions thrown in an errback but reject the derived promise',
function() {
var error1 = error(1, 'oops', true);
promise.then(null, error1).then(success(2), error(2)).catch(noop);
syncReject(deferred, 'nope');
expect(logStr()).toBe('error1(nope)->throw(oops); error2(oops)->reject(oops)');
expect(mockExceptionLogger.log).toEqual(['oops']);
expect(mockExceptionLogger.log).toEqual([]);
});
@@ -2127,13 +2128,13 @@ describe('q', function() {
describe('in when', function() {
it('should log exceptions thrown in a success callback and reject the derived promise',
it('should NOT log exceptions thrown in a success callback but reject the derived promise',
function() {
var success1 = success(1, 'oops', true);
q.when('hi', success1, error()).then(success(), error(2)).catch(noop);
mockNextTick.flush();
expect(logStr()).toBe('success1(hi)->throw(oops); error2(oops)->reject(oops)');
expect(mockExceptionLogger.log).toEqual(['oops']);
expect(mockExceptionLogger.log).toEqual([]);
});
@@ -2145,12 +2146,12 @@ describe('q', function() {
});
it('should log exceptions thrown in a errback and reject the derived promise', function() {
it('should NOT log exceptions thrown in a errback but reject the derived promise', function() {
var error1 = error(1, 'oops', true);
q.when(q.reject('sorry'), success(), error1).then(success(), error(2)).catch(noop);
mockNextTick.flush();
expect(logStr()).toBe('error1(sorry)->throw(oops); error2(oops)->reject(oops)');
expect(mockExceptionLogger.log).toEqual(['oops']);
expect(mockExceptionLogger.log).toEqual([]);
});
@@ -2207,50 +2208,4 @@ describe('q', function() {
expect(exceptionHandlerStr()).toBe('');
});
});
describe('when exceptionHandler rethrows exceptions, ', function() {
var originalLogExceptions, deferred, errorSpy, exceptionExceptionSpy;
beforeEach(function() {
// Turn off exception logging for these particular tests
originalLogExceptions = mockNextTick.logExceptions;
mockNextTick.logExceptions = false;
// Set up spies
exceptionExceptionSpy = jasmine.createSpy('rethrowExceptionHandler')
.and.callFake(function rethrowExceptionHandler(e) {
throw e;
});
errorSpy = jasmine.createSpy('errorSpy');
q = qFactory(mockNextTick.nextTick, exceptionExceptionSpy);
deferred = q.defer();
});
afterEach(function() {
// Restore the original exception logging mode
mockNextTick.logExceptions = originalLogExceptions;
});
it('should still reject the promise, when exception is thrown in success handler, even if exceptionHandler rethrows', function() {
deferred.promise.then(function() { throw new Error('reject'); }).then(null, errorSpy);
deferred.resolve('resolve');
mockNextTick.flush();
expect(exceptionExceptionSpy).toHaveBeenCalled();
expect(errorSpy).toHaveBeenCalled();
});
it('should still reject the promise, when exception is thrown in error handler, even if exceptionHandler rethrows', function() {
deferred.promise.then(null, function() { throw new Error('reject again'); }).then(null, errorSpy);
deferred.reject('reject');
mockNextTick.flush();
expect(exceptionExceptionSpy).toHaveBeenCalled();
expect(errorSpy).toHaveBeenCalled();
});
});
});
+46 -36
View File
@@ -114,49 +114,59 @@ describe('$templateRequest', function() {
expect($templateCache.get('tpl.html')).toBe('matias');
}));
it('should throw an error when the template is not found',
inject(function($rootScope, $templateRequest, $httpBackend) {
it('should call `$exceptionHandler` on request error', function() {
module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
});
$httpBackend.expectGET('tpl.html').respond(404, '', {}, 'Not found');
$templateRequest('tpl.html');
$rootScope.$digest();
expect(function() {
$rootScope.$digest();
$httpBackend.flush();
}).toThrowMinErr('$compile', 'tpload', 'Failed to load template: tpl.html (HTTP status: 404 Not found)');
}));
it('should not throw when the template is not found and ignoreRequestError is true',
inject(function($rootScope, $templateRequest, $httpBackend) {
$httpBackend.expectGET('tpl.html').respond(404);
inject(function($exceptionHandler, $httpBackend, $templateRequest) {
$httpBackend.expectGET('tpl.html').respond(404, '', {}, 'Not Found');
var err;
$templateRequest('tpl.html', true).catch(function(reason) { err = reason; });
$templateRequest('tpl.html').catch(function(reason) { err = reason; });
$httpBackend.flush();
expect(err.message).toMatch(new RegExp(
'^\\[\\$compile:tpload] Failed to load template: tpl\\.html ' +
'\\(HTTP status: 404 Not Found\\)'));
expect($exceptionHandler.errors[0].message).toMatch(new RegExp(
'^\\[\\$compile:tpload] Failed to load template: tpl\\.html ' +
'\\(HTTP status: 404 Not Found\\)'));
});
});
it('should not call `$exceptionHandler` on request error when `ignoreRequestError` is true',
function() {
module(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
});
inject(function($exceptionHandler, $httpBackend, $templateRequest) {
$httpBackend.expectGET('tpl.html').respond(404);
var err;
$templateRequest('tpl.html', true).catch(function(reason) { err = reason; });
$httpBackend.flush();
expect(err.status).toBe(404);
expect($exceptionHandler.errors).toEqual([]);
});
}
);
it('should not call `$exceptionHandler` when the template is empty',
inject(function($exceptionHandler, $httpBackend, $rootScope, $templateRequest) {
$httpBackend.expectGET('tpl.html').respond('');
var onError = jasmine.createSpy('onError');
$templateRequest('tpl.html').catch(onError);
$rootScope.$digest();
$httpBackend.flush();
expect(err.status).toBe(404);
}));
it('should not throw an error when the template is empty',
inject(function($rootScope, $templateRequest, $httpBackend) {
$httpBackend.expectGET('tpl.html').respond('');
$templateRequest('tpl.html');
$rootScope.$digest();
expect(function() {
$rootScope.$digest();
$httpBackend.flush();
}).not.toThrow();
}));
expect(onError).not.toHaveBeenCalled();
expect($exceptionHandler.errors).toEqual([]);
})
);
it('should accept empty templates and refuse null or undefined templates in cache',
inject(function($rootScope, $templateRequest, $templateCache, $sce) {
+21 -11
View File
@@ -782,11 +782,20 @@ describe('$route', function() {
});
inject(function($route, $location, $rootScope) {
var onError = jasmine.createSpy('onError');
var onSuccess = jasmine.createSpy('onSuccess');
$rootScope.$on('$routeChangeError', onError);
$rootScope.$on('$routeChangeSuccess', onSuccess);
$location.path('/foo');
expect(function() {
$rootScope.$digest();
}).toThrowMinErr('$sce', 'insecurl', 'Blocked loading resource from url not allowed by ' +
'$sceDelegate policy. URL: http://example.com/foo.html');
$rootScope.$digest();
expect(onSuccess).not.toHaveBeenCalled();
expect(onError).toHaveBeenCalled();
expect(onError.calls.mostRecent().args[3].message).toMatch(new RegExp(
'^\\[\\$sce:insecurl] Blocked loading resource from url not allowed by ' +
'\\$sceDelegate policy\\. URL: http:\\/\\/example\\.com\\/foo\\.html'));
});
});
@@ -903,8 +912,7 @@ describe('$route', function() {
it('should catch local factory errors', function() {
var myError = new Error('MyError');
module(function($routeProvider, $exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
module(function($routeProvider) {
$routeProvider.when('/locals', {
resolve: {
a: function($q) {
@@ -914,10 +922,14 @@ describe('$route', function() {
});
});
inject(function($location, $route, $rootScope, $exceptionHandler) {
inject(function($location, $route, $rootScope) {
spyOn($rootScope, '$broadcast').and.callThrough();
$location.path('/locals');
$rootScope.$digest();
expect($exceptionHandler.errors).toEqual([myError]);
expect($rootScope.$broadcast).toHaveBeenCalledWith(
'$routeChangeError', jasmine.any(Object), undefined, myError);
});
});
});
@@ -1182,8 +1194,7 @@ describe('$route', function() {
it('should broadcast `$routeChangeError` when redirectTo throws', function() {
var error = new Error('Test');
module(function($exceptionHandlerProvider, $routeProvider) {
$exceptionHandlerProvider.mode('log');
module(function($routeProvider) {
$routeProvider.when('/foo', {redirectTo: function() { throw error; }});
});
@@ -1196,7 +1207,6 @@ describe('$route', function() {
var lastCallArgs = $rootScope.$broadcast.calls.mostRecent().args;
expect(lastCallArgs[0]).toBe('$routeChangeError');
expect(lastCallArgs[3]).toBe(error);
expect($exceptionHandler.errors[0]).toBe(error);
});
});