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

Commit a4a4b82

Browse files
DavidSoutherpetebacondarwin
authored andcommitted
feat(ngMock/$exceptionHandler): log errors when rethrowing
Now the `rethrow` mode will also record a log of the error in the same way as the `log` mode. Closes #10540 Closes #10564 Conflicts: src/ngMock/angular-mocks.js
1 parent d60fbcc commit a4a4b82

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/ngMock/angular-mocks.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -233,32 +233,31 @@ angular.mock.$ExceptionHandlerProvider = function() {
233233
*
234234
* @param {string} mode Mode of operation, defaults to `rethrow`.
235235
*
236-
* - `rethrow`: If any errors are passed into the handler in tests, it typically
237-
* means that there is a bug in the application or test, so this mock will
238-
* make these tests fail.
239236
* - `log`: Sometimes it is desirable to test that an error is thrown, for this case the `log`
240237
* mode stores an array of errors in `$exceptionHandler.errors`, to allow later
241238
* assertion of them. See {@link ngMock.$log#assertEmpty assertEmpty()} and
242239
* {@link ngMock.$log#reset reset()}
240+
* - `rethrow`: If any errors are passed to the handler in tests, it typically means that there
241+
* is a bug in the application or test, so this mock will make these tests fail.
242+
* For any implementations that expect exceptions to be thrown, the `rethrow` mode
243+
* will also maintain a log of thrown errors.
243244
*/
244245
this.mode = function(mode) {
245-
switch(mode) {
246-
case 'rethrow':
247-
handler = function(e) {
248-
throw e;
249-
};
250-
break;
246+
247+
switch (mode) {
251248
case 'log':
249+
case 'rethrow':
252250
var errors = [];
253-
254251
handler = function(e) {
255252
if (arguments.length == 1) {
256253
errors.push(e);
257254
} else {
258255
errors.push([].slice.call(arguments, 0));
259256
}
257+
if (mode === "rethrow") {
258+
throw e;
259+
}
260260
};
261-
262261
handler.errors = errors;
263262
break;
264263
default:

test/ngMock/angular-mocksSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,19 @@ describe('ngMock', function() {
601601
});
602602
});
603603

604+
it('should log and rethrow exceptions', function() {
605+
module(function($exceptionHandlerProvider) {
606+
$exceptionHandlerProvider.mode('rethrow');
607+
});
608+
inject(function($exceptionHandler) {
609+
expect(function() { $exceptionHandler('MyError'); }).toThrow('MyError');
610+
expect($exceptionHandler.errors).toEqual(['MyError']);
611+
612+
expect(function() { $exceptionHandler('MyError', 'comment'); }).toThrow('MyError');
613+
expect($exceptionHandler.errors[1]).toEqual(['MyError', 'comment']);
614+
});
615+
});
616+
604617
it('should throw on wrong argument', function() {
605618
module(function($exceptionHandlerProvider) {
606619
expect(function() {

0 commit comments

Comments
 (0)