Skip to content

Commit

Permalink
Merge pull request #181 from sinkingrowboats/master
Browse files Browse the repository at this point in the history
added error handling override function
  • Loading branch information
j3tan authored Aug 26, 2016
2 parents d4ad70c + 9e0bee4 commit 74c62ad
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -135,7 +136,10 @@ Box.Application = (function() {
* @private
*/
function error(exception) {

if (typeof customErrorHandler === 'function') {
customErrorHandler(exception);
return;
}
if (globalConfig.debug) {
throw exception;
} else {
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions tests/application-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});

});

});

});

0 comments on commit 74c62ad

Please sign in to comment.