Skip to content

Commit a714dd0

Browse files
Refactored decorator into a factory so that we can test that the delegate is called
1 parent febd55e commit a714dd0

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed
+17-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
angular.module('services.exceptionHandler', ['services.notifications', 'services.localizedMessages'], ['$provide', function($provide) {
2-
3-
$provide.decorator('$exceptionHandler', ['$injector', '$delegate', 'localizedMessages', function ($injector, $delegate, localizedMessages) {
1+
angular.module('services.exceptionHandler', ['services.notifications', 'services.localizedMessages']);
42

3+
angular.module('services.exceptionHandler').factory('exceptionHandlerFactory', ['$injector', 'localizedMessages', function($injector, localizedMessages) {
4+
return function($delegate) {
5+
var notifications;
56
return function (exception, cause) {
6-
//need to get the notications service from injector due to circular dependencies:
7+
// Lazy load notifications to get around circular dependency
78
//Circular dependency: $rootScope <- notifications <- $exceptionHandler
9+
var notifications = notifications || $injector.get('notifications');
10+
11+
// Pass through to original handler
812
$delegate(exception, cause);
9-
$injector.get('notifications').pushForCurrentRoute(localizedMessages.get('error.fatal'), 'error', {
13+
14+
// Push a notification error
15+
notifications.pushForCurrentRoute(localizedMessages.get('error.fatal'), 'error', {
1016
exception:exception,
1117
cause:cause
1218
});
1319
};
20+
};
21+
}]);
22+
23+
angular.module('services.exceptionHandler').config(['$provide', function($provide) {
24+
$provide.decorator('$exceptionHandler', ['$delegate', 'exceptionHandlerFactory', function ($delegate, exceptionHandlerFactory) {
25+
return exceptionHandlerFactory($delegate);
1426
}]);
1527
}]);

test/unit/common/services/exceptionHandlerSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,16 @@ describe('exception handler', function () {
3636
$exceptionHandler(new Error('root cause'));
3737
}).toThrow('issue with notifications');
3838
});
39+
40+
it('should call through to the delegate', function() {
41+
inject(function(exceptionHandlerFactory) {
42+
var error = new Error('Something went wrong...');
43+
var cause = 'Some obscure problem...';
44+
45+
var delegate = jasmine.createSpy('delegate');
46+
var exceptionHandler = exceptionHandlerFactory(delegate);
47+
exceptionHandler(error, cause);
48+
expect(delegate).toHaveBeenCalledWith(error, cause);
49+
});
50+
});
3951
});

0 commit comments

Comments
 (0)