mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-01-12 17:12:34 +08:00
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
This commit is contained in:
committed by
Peter Bacon Darwin
parent
b43fa3bb30
commit
deb3cb4dae
22
src/ngMock/angular-mocks.js
vendored
22
src/ngMock/angular-mocks.js
vendored
@@ -243,31 +243,31 @@ angular.mock.$ExceptionHandlerProvider = function() {
|
||||
*
|
||||
* @param {string} mode Mode of operation, defaults to `rethrow`.
|
||||
*
|
||||
* - `rethrow`: If any errors are passed to the handler in tests, it typically means that there
|
||||
* is a bug in the application or test, so this mock will make these tests fail.
|
||||
* - `log`: Sometimes it is desirable to test that an error is thrown, for this case the `log`
|
||||
* mode stores an array of errors in `$exceptionHandler.errors`, to allow later
|
||||
* assertion of them. See {@link ngMock.$log#assertEmpty assertEmpty()} and
|
||||
* {@link ngMock.$log#reset reset()}
|
||||
* - `rethrow`: If any errors are passed to the handler in tests, it typically means that there
|
||||
* is a bug in the application or test, so this mock will make these tests fail.
|
||||
* For any implementations that expect exceptions to be thrown, the `rethrow` mode
|
||||
* will also maintain a log of thrown errors.
|
||||
*/
|
||||
this.mode = function(mode) {
|
||||
switch (mode) {
|
||||
case 'rethrow':
|
||||
handler = function(e) {
|
||||
throw e;
|
||||
};
|
||||
break;
|
||||
case 'log':
|
||||
var errors = [];
|
||||
|
||||
switch (mode) {
|
||||
case 'log':
|
||||
case 'rethrow':
|
||||
var errors = [];
|
||||
handler = function(e) {
|
||||
if (arguments.length == 1) {
|
||||
errors.push(e);
|
||||
} else {
|
||||
errors.push([].slice.call(arguments, 0));
|
||||
}
|
||||
if (mode === "rethrow") {
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
handler.errors = errors;
|
||||
break;
|
||||
default:
|
||||
|
||||
13
test/ngMock/angular-mocksSpec.js
vendored
13
test/ngMock/angular-mocksSpec.js
vendored
@@ -605,6 +605,19 @@ describe('ngMock', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('should log and rethrow exceptions', function() {
|
||||
module(function($exceptionHandlerProvider) {
|
||||
$exceptionHandlerProvider.mode('rethrow');
|
||||
});
|
||||
inject(function($exceptionHandler) {
|
||||
expect(function() { $exceptionHandler('MyError'); }).toThrow('MyError');
|
||||
expect($exceptionHandler.errors).toEqual(['MyError']);
|
||||
|
||||
expect(function() { $exceptionHandler('MyError', 'comment'); }).toThrow('MyError');
|
||||
expect($exceptionHandler.errors[1]).toEqual(['MyError', 'comment']);
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw on wrong argument', function() {
|
||||
module(function($exceptionHandlerProvider) {
|
||||
expect(function() {
|
||||
|
||||
Reference in New Issue
Block a user