bv-ui-core
Advanced tools
Comparing version 2.1.1 to 2.2.0
@@ -22,2 +22,19 @@ /* eslint-disable */ | ||
/** | ||
* By default, we should simply log the error to the console. | ||
*/ | ||
function defaultErrorHandler(e) { | ||
global.console.error(e); | ||
} | ||
/** | ||
* Initialize a custom error handler for this instance, which may decide to log or even re-throw the error. | ||
* | ||
* @param {Function} fn - the custom callback to be invoked whenever an error is | ||
* detected as the result of a trigger event | ||
*/ | ||
function setErrorHandler(fn) { | ||
this.eventErrorHandler = fn; | ||
} | ||
function bind(event, fn) { | ||
@@ -82,3 +99,8 @@ var i, part; | ||
} | ||
catch (e) {} | ||
catch (e) { | ||
(this.eventErrorHandler || defaultErrorHandler).call(this, e, { | ||
event: event, | ||
data: args | ||
}); | ||
} | ||
} | ||
@@ -111,2 +133,3 @@ return this; | ||
this.one = this.once = one; | ||
this.setErrorHandler = setErrorHandler; | ||
@@ -113,0 +136,0 @@ return this; |
@@ -24,2 +24,20 @@ # evented | ||
By default, any errors thrown from an event listener are caught and simply logged to the console. | ||
An alternate behavior can be specified by providing an event handler: | ||
```js | ||
var model = new Model({}); | ||
model.setErrorHandler(function(error, trigger) { | ||
// The following parameters are available to this function: | ||
// - error : the value that was thrown, typically an instance of Error | ||
// - trigger.event : the name of the event passed to trigger() | ||
// - trigger.data : an array containing the additional arguments passed to trigger() | ||
// - this : the instance on which trigger() was invoked | ||
}); | ||
// If you prefer for errors to be thrown from trigger(), then use: | ||
model.setErrorHandler(function(error) { throw error; }); | ||
``` | ||
[1]: https://github.com/mkuklis/asEvented |
@@ -10,4 +10,4 @@ /** | ||
if (typeof globalThis !== 'undefined') { return globalThis; } | ||
if (typeof window !== 'undefined') { return window; } | ||
if (typeof self !== 'undefined') { return self; } | ||
if (typeof window !== 'undefined') { return window; } | ||
if (typeof global !== 'undefined') { return global; } | ||
@@ -14,0 +14,0 @@ throw new Error('unable to locate global object'); |
{ | ||
"name": "bv-ui-core", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"license": "Apache 2.0", | ||
@@ -5,0 +5,0 @@ "description": "Bazaarvoice UI-related JavaScript", |
@@ -29,2 +29,32 @@ /** | ||
it('invokes an errorHandler function correctly', function () { | ||
function M () {} | ||
evented.call(M.prototype); | ||
var m = new M(); | ||
// Ensure that errors escaping from listeners are caught | ||
var error = new Error('test error 1'); | ||
m.on('test_event', function () { | ||
throw error; | ||
}); | ||
m.trigger('test_event'); // should NOT throw | ||
// Ensure that errors are provided to custom error handler | ||
error = new Error('test error 2'); | ||
var spy = sinon.spy(); | ||
m.setErrorHandler(spy); | ||
m.trigger('test_event'); | ||
expect(spy).to.have.been.calledWith(error, sinon.match.has('event', 'test_event')); | ||
// Ensure that errors can be rethrown from custom error handler | ||
error = new Error('test error 3'); | ||
m.setErrorHandler(function (theError) { | ||
throw theError; | ||
}); | ||
expect(function () { | ||
m.trigger('test_event'); | ||
}).to.throw(error); | ||
}) | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
219353
5180
7