npm

Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »

modify-error-event

1.0.0 • Public • Published

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.

install

npm i modify-error-event

Downloadsweekly downloads

1,134

version

1.0.0

license

none

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability