modify-error-event

Modify the value of the specific object's error event

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
modify-error-event
001.0.08 years ago8 years agoMinified + gzip package size for modify-error-event in KB

Readme

modify-error-event
NPM version Build Status Build status Coverage Status Dependency Status devDependency Status
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

Use npm.
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.