From 84823b2eff21060c0883f6b4da5573380381c1a2 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Wed, 14 Dec 2011 09:28:16 +0100 Subject: [PATCH] feature($exceptionHandler): $exceptionHandler now supports var_args --- src/angular-mocks.js | 8 ++++++-- src/service/exceptionHandler.js | 12 ++++++++---- test/angular-mocksSpec.js | 3 +++ test/service/exceptionHandlerSpec.js | 25 +++++++++++++++++-------- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/angular-mocks.js b/src/angular-mocks.js index 516259b88695..0645322b44fe 100644 --- a/src/angular-mocks.js +++ b/src/angular-mocks.js @@ -249,8 +249,12 @@ angular.mock.$ExceptionHandlerProvider = function() { case 'log': var errors = []; handler = function(e) { - errors.push(e); - }; + if (arguments.length == 1) { + errors.push(e); + } else { + errors.push([].slice.call(arguments, 0)); + } + } handler.errors = errors; break; default: diff --git a/src/service/exceptionHandler.js b/src/service/exceptionHandler.js index 6d680b0463b2..26ea58454639 100644 --- a/src/service/exceptionHandler.js +++ b/src/service/exceptionHandler.js @@ -10,13 +10,17 @@ * The default implementation simply delegates to `$log.error` which logs it into * the browser console. * - * In unit tests, if `angular-mocks.js` is loaded, this service is overriden by + * In unit tests, if `angular-mocks.js` is loaded, this service is overridden by * {@link angular.module.ngMock.$exceptionHandler mock $exceptionHandler} + * + * @param {Error} exception Exception associated with the error. + * @param {string=} cause optional information about the context in which + * the error was thrown. */ -function $ExceptionHandlerProvider(){ +function $ExceptionHandlerProvider() { this.$get = ['$log', function($log){ - return function(e) { - $log.error(e); + return function(exception, cause) { + $log.error.apply($log, arguments); }; }]; } diff --git a/test/angular-mocksSpec.js b/test/angular-mocksSpec.js index 073d6cd1ff11..2527ca4885b8 100644 --- a/test/angular-mocksSpec.js +++ b/test/angular-mocksSpec.js @@ -307,6 +307,9 @@ describe('ngMock', function() { var $exceptionHandler = $exceptionHandlerProvider.$get(); $exceptionHandler('MyError'); expect($exceptionHandler.errors).toEqual(['MyError']); + + $exceptionHandler('MyError', 'comment'); + expect($exceptionHandler.errors[1]).toEqual(['MyError', 'comment']); })); diff --git a/test/service/exceptionHandlerSpec.js b/test/service/exceptionHandlerSpec.js index 2f3d0e665192..4ab946798878 100644 --- a/test/service/exceptionHandlerSpec.js +++ b/test/service/exceptionHandlerSpec.js @@ -1,15 +1,24 @@ 'use strict'; describe('$exceptionHandler', function() { + it('should log errors with single argument', function() { + module(function($provide){ + $provide.service('$exceptionHandler', $ExceptionHandlerProvider); + }); + inject(function($log, $exceptionHandler) { + $exceptionHandler('myError'); + expect($log.error.logs.shift()).toEqual(['myError']); + }); + }); - it('should log errors', function() { - module(function($provide){ - $provide.service('$exceptionHandler', $ExceptionHandlerProvider); - }); - inject(function($log, $exceptionHandler) { - $exceptionHandler('myError'); - expect($log.error.logs.shift()).toEqual(['myError']); + it('should log errors with multiple arguments', function() { + module(function($provide){ + $provide.service('$exceptionHandler', $ExceptionHandlerProvider); + }); + inject(function($log, $exceptionHandler) { + $exceptionHandler('myError', 'comment'); + expect($log.error.logs.shift()).toEqual(['myError', 'comment']); + }); }); - }); });