Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fixup! fix($q): treat thrown errors as regular rejections
Browse files Browse the repository at this point in the history
  • Loading branch information
gkalpak committed Oct 5, 2016
1 parent 58cc8a5 commit 0c4266d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
10 changes: 4 additions & 6 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
68 changes: 40 additions & 28 deletions test/ng/templateRequestSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0c4266d

Please sign in to comment.