From 6eb7e15b112901970b499c5d8072b8927c6f0c37 Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Fri, 20 Jan 2017 14:31:24 +0200 Subject: [PATCH] fix($compile): do not swallow thrown errors in test In e13eeab, errors/rejections produces during fetching the template or compiling an asynchronous directive, where overzealously silenced. This doesn't make any difference in (most) production apps, where `$exceptionHandler` does not rethrow the errors. In tests though (where `$exceptionHandler` rethrows by default), it can unexpectedly "swallow" thrown errors. This commit fixes it by removing the extraneous `.catch(noop)`, thus letting errors thrown by `$exceptionHandler` to surface. The changes in 'compileSpec.js' essentially revert the modifications that were unnecessarily (and incorrectly) done in e13eeab (and also one incorrect modification from [c22615c][1]). [1]: https://github.com/angular/angular.js/commit/c22615cbfbaa7d1712e79b6bf2ace6eb41313bac#diff-348c2f3781ed66a24894c2046a52c628L2084 Fixes #15629 --- src/ng/compile.js | 2 +- test/ng/compileSpec.js | 64 ++++++++++++++++++++---------------------- 2 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index 5c02a0f0be63..66f685ebafbf 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -3156,7 +3156,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { if (error instanceof Error) { $exceptionHandler(error); } - }).catch(noop); + }); return function delayedNodeLinkFn(ignoreChildLinkFn, scope, node, rootElement, boundTranscludeFn) { var childBoundTranscludeFn = boundTranscludeFn; diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 2ab6d6f13eb5..9323caf692e2 100644 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -1881,15 +1881,14 @@ describe('$compile', function() { it('should throw an error and clear element content if the template fails to load', - inject(function($compile, $exceptionHandler, $httpBackend, $rootScope) { + inject(function($compile, $httpBackend, $rootScope) { $httpBackend.expect('GET', 'hello.html').respond(404, 'Not Found!'); element = $compile('
content
')($rootScope); - $httpBackend.flush(); - + expect(function() { + $httpBackend.flush(); + }).toThrowMinErr('$compile', 'tpload', 'Failed to load template: hello.html'); expect(sortedHtml(element)).toBe('
'); - expect($exceptionHandler.errors[0]).toEqualMinErr('$compile', 'tpload', - 'Failed to load template: hello.html'); }) ); @@ -1905,13 +1904,13 @@ describe('$compile', function() { templateUrl: 'template.html' })); }); - inject(function($compile, $exceptionHandler, $httpBackend) { + inject(function($compile, $httpBackend) { $httpBackend.whenGET('template.html').respond('

template.html

'); - $compile('
'); - $httpBackend.flush(); - - expect($exceptionHandler.errors[0]).toEqualMinErr('$compile', 'multidir', + expect(function() { + $compile('
'); + $httpBackend.flush(); + }).toThrowMinErr('$compile', 'multidir', 'Multiple directives [async, sync] asking for template on: ' + '
'); }); @@ -2122,15 +2121,15 @@ describe('$compile', function() { 'multiple root elements': '
' }, function(directiveTemplate) { - inject(function($compile, $templateCache, $rootScope, $exceptionHandler) { + inject(function($compile, $templateCache, $rootScope) { $templateCache.put('template.html', directiveTemplate); - $compile('

')($rootScope); - $rootScope.$digest(); - expect($exceptionHandler.errors.pop()).toEqualMinErr('$compile', 'tplrt', - 'Template for directive \'template\' must have exactly one root element. ' + - 'template.html' - ); + expect(function() { + $compile('

')($rootScope); + $rootScope.$digest(); + }).toThrowMinErr('$compile', 'tplrt', + 'Template for directive \'template\' must have exactly one root element. ' + + 'template.html'); }); }); @@ -2657,13 +2656,13 @@ describe('$compile', function() { ); it('should not allow more than one isolate/new scope creation per element regardless of `templateUrl`', - inject(function($exceptionHandler, $httpBackend) { + inject(function($httpBackend) { $httpBackend.expect('GET', 'tiscope.html').respond('
Hello, world !
'); - compile('
'); - $httpBackend.flush(); - - expect($exceptionHandler.errors[0]).toEqualMinErr('$compile', 'multidir', + expect(function() { + compile('
'); + $httpBackend.flush(); + }).toThrowMinErr('$compile', 'multidir', 'Multiple directives [scopeB, tiscopeA] asking for new/isolated scope on: ' + '
'); }) @@ -8997,18 +8996,17 @@ describe('$compile', function() { })); }); - inject(function($compile, $exceptionHandler, $rootScope, $templateCache) { + inject(function($compile, $rootScope, $templateCache) { $templateCache.put('noTransBar.html', '
' + // This ng-transclude is invalid. It should throw an error. '
' + '
'); - element = $compile('
content
')($rootScope); - $rootScope.$digest(); - - expect($exceptionHandler.errors[0][1]).toBe('
'); - expect($exceptionHandler.errors[0][0]).toEqualMinErr('ngTransclude', 'orphan', + expect(function() { + element = $compile('
content
')($rootScope); + $rootScope.$digest(); + }).toThrowMinErr('ngTransclude', 'orphan', 'Illegal use of ngTransclude directive in the template! ' + 'No parent directive that requires a transclusion found. ' + 'Element:
'); @@ -9821,13 +9819,13 @@ describe('$compile', function() { transclude: 'element' })); }); - inject(function($compile, $exceptionHandler, $httpBackend) { + inject(function($compile, $httpBackend) { $httpBackend.expectGET('template.html').respond('

template.html

'); - $compile('
'); - $httpBackend.flush(); - - expect($exceptionHandler.errors[0]).toEqualMinErr('$compile', 'multidir', + expect(function() { + $compile('
'); + $httpBackend.flush(); + }).toThrowMinErr('$compile', 'multidir', 'Multiple directives [first, second] asking for transclusion on: