core: catch errors throw from presets at pipe()

This commit is contained in:
Micooz 2017-10-15 12:14:56 +08:00
parent ba16b706d1
commit 0c297c9a92

@ -1,6 +1,7 @@
import EventEmitter from 'events';
import {MIDDLEWARE_DIRECTION_UPWARD} from './middleware';
import {PRESET_FAILED} from '../presets/defs';
import {logger} from '../utils';
// .on('broadcast')
// .on(`pre_${direction}`)
@ -65,14 +66,18 @@ export class Pipe extends EventEmitter {
}
feed(direction, buffer) {
// cache the current buffer for PRESET_FAILED action
this._cacheBuffer = buffer;
// pre-feed hook
const preEventName = `pre_${direction}`;
if (this.listenerCount(preEventName) > 0) {
this.emit(preEventName, buffer, (buf) => this._feed(direction, buf));
} else {
this._feed(direction, buffer);
try {
// cache the current buffer for PRESET_FAILED action
this._cacheBuffer = buffer;
// pre-feed hook
const preEventName = `pre_${direction}`;
if (this.listenerCount(preEventName) > 0) {
this.emit(preEventName, buffer, (buf) => this._feed(direction, buf));
} else {
this._feed(direction, buffer);
}
} catch (err) {
logger.error(`[pipe] error occurred while piping: ${err.message}`);
}
}