modify-error-event
v1.0.0
Published
Modify the value of the specific object's error event
Downloads
1,952
Maintainers
Readme
modify-error-event
Modify the value of the specific object's error
event
var EventEmitter = require('events').EventEmitter;
var modifyErrorEvent = require('modify-error-event');
var emitter = new EventEmitter();
modifyErrorEvent(emitter, function(err) {
err.message = 'bar';
return err;
});
emitter.on('error', function(err) {
err.message; //=> 'bar'
});
emitter.emit('error', new Error('foo'));
Installation
npm install modify-error-event
API
var modifyErrorEvent = require('modify-error-event');
modifyErrorEvent(eventEmitter, modifier)
eventEmitter: Object
(an instance of EventEmitter
or its inheritance e.g. Stream
)
modifier: Function
Return: Object
(Same as the first argument)
It changes the first argument of the error
event listeners in response to the return value of the modifier function.
var EventEmitter = require('events').EventEmitter;
var modifyErrorEvent = require('modify-error-event');
var emitter = new EventEmitter();
modifyErrorEvent(emitter, function(err) {
err.message += 'b';
return err;
});
modifyErrorEvent(emitter, function(val) {
err.message += 'c';
return err;
});
emitter
.on('error', listener)
.emit('error', new Error('a'));
function listener(err) {
err.message; //=> 'abc'
}
License
Copyright (c) 2015 Shinnosuke Watanabe
Licensed under the MIT License.