diff --git a/src/ng/compile.js b/src/ng/compile.js index 67496c981e67..d264d6689684 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -3052,8 +3052,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { $compileNode.empty(); - $templateRequest(templateUrl). - then(function(content) { + $templateRequest(templateUrl) + .then(function(content) { var compileNode, tempTemplateAttrs, $template, childBoundTranscludeFn; content = denormalizeTemplate(content); @@ -3131,13 +3131,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { childBoundTranscludeFn); } linkQueue = null; - }). - catch(function(error) { + }).catch(function(error) { if (error instanceof Error) { $exceptionHandler(error); } - }). - catch(noop); + }).catch(noop); return function delayedNodeLinkFn(ignoreChildLinkFn, scope, node, rootElement, boundTranscludeFn) { var childBoundTranscludeFn = boundTranscludeFn; diff --git a/test/ng/templateRequestSpec.js b/test/ng/templateRequestSpec.js index 4bf426700ca2..5c741e5e52a6 100644 --- a/test/ng/templateRequestSpec.js +++ b/test/ng/templateRequestSpec.js @@ -114,47 +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($exceptionHandler, $httpBackend, $rootScope, $templateRequest) { - $httpBackend.expectGET('tpl.html').respond(404, '', {}, 'Not found'); + it('should call `$exceptionHandler` on request error', function() { + module(function($exceptionHandlerProvider) { + $exceptionHandlerProvider.mode('log'); + }); - $templateRequest('tpl.html').catch(noop); + inject(function($exceptionHandler, $httpBackend, $templateRequest) { + $httpBackend.expectGET('tpl.html').respond(404, '', {}, 'Not Found'); + + var err; + $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 throw when the template is not found and ignoreRequestError is true', - inject(function($rootScope, $templateRequest, $httpBackend) { - - $httpBackend.expectGET('tpl.html').respond(404); - - var err; - $templateRequest('tpl.html', true).catch(function(reason) { err = reason; }); - - $rootScope.$digest(); - $httpBackend.flush(); + '\\(HTTP status: 404 Not Found\\)')); + }); + }); - expect(err.status).toBe(404); - })); + it('should not call `$exceptionHandler` on request error when `ignoreRequestError` is true', + function() { + module(function($exceptionHandlerProvider) { + $exceptionHandlerProvider.mode('log'); + }); - it('should not throw an error when the template is empty', - inject(function($rootScope, $templateRequest, $httpBackend) { + inject(function($exceptionHandler, $httpBackend, $templateRequest) { + $httpBackend.expectGET('tpl.html').respond(404); - $httpBackend.expectGET('tpl.html').respond(''); + var err; + $templateRequest('tpl.html', true).catch(function(reason) { err = reason; }); + $httpBackend.flush(); - $templateRequest('tpl.html'); + expect(err.status).toBe(404); + expect($exceptionHandler.errors).toEqual([]); + }); + } + ); - $rootScope.$digest(); + it('should not call `$exceptionHandler` when the template is empty', + inject(function($exceptionHandler, $httpBackend, $rootScope, $templateRequest) { + $httpBackend.expectGET('tpl.html').respond(''); - expect(function() { + var onError = jasmine.createSpy('onError'); + $templateRequest('tpl.html').catch(onError); $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) {