diff --git a/lib/application.js b/lib/application.js index e636fad..7d0b7df 100644 --- a/lib/application.js +++ b/lib/application.js @@ -52,6 +52,7 @@ Box.Application = (function() { behaviors = {}, // Information about each registered behavior by behaviorName instances = {}, // Module instances keyed by DOM element id initialized = false, // Flag whether the application has been initialized + customErrorHandler = null, application = new Box.EventTarget(); // base object for application @@ -135,7 +136,10 @@ Box.Application = (function() { * @private */ function error(exception) { - + if (typeof customErrorHandler === 'function') { + customErrorHandler(exception); + return; + } if (globalConfig.debug) { throw exception; } else { @@ -810,6 +814,16 @@ Box.Application = (function() { // Error reporting //---------------------------------------------------------------------- + /** + * Overrides default error handler + * @param {Function} exceptionHandler handling function that takes an + * exception as argument. Must be called before init. + * @returns {void} + */ + setErrorHandler: function(exceptionHandler) { + customErrorHandler = exceptionHandler; + }, + /** * Signals that an error has occurred. If in development mode, an error * is thrown. If in production mode, an event is fired. diff --git a/tests/application-test.js b/tests/application-test.js index dab8f77..a8efd68 100644 --- a/tests/application-test.js +++ b/tests/application-test.js @@ -1097,6 +1097,25 @@ describe('Box.Application', function() { }); + describe('setErrorHandler()', function() { + + afterEach(function() { + Box.Application.setErrorHandler(undefined); + }); + + it('should call a custom error handler when one is provided', function() { + var customErrorHandler = sandbox.spy(); + var error = new Error('blah'); + + Box.Application.setErrorHandler(customErrorHandler); + Box.Application.init(); + Box.Application.reportError(error); + + assert(customErrorHandler.calledWith(error)); + }); + + }); + }); });