不要怂,就是干,撸起袖子干!

Commit 428a4e85 by Sascha Gehlich

Fix CustomEventEmitter error handling

Override .emit() to first check whether the error argument is an array. In case
it is an array, reduce it to the very first error and pass it to the original
EventEmitter.emit()
1 parent 8babe6ca
......@@ -28,6 +28,25 @@ module.exports = (function() {
return this
}
CustomEventEmitter.prototype.emit = function(type) {
this._events = this._events || {};
// Override default 'error' event logic
if (type === 'error' && !this._events.error) {
// No error listener
var er = arguments[1];
// If error argument is an array, make sure we
// pass only the first error to the original
// .emit() function of EventEmitter
if (er instanceof Array) {
arguments[1] = Utils._.flatten(er)[0]
}
}
EventEmitter.prototype.emit.apply(this, arguments);
};
/**
Shortcut methods (success, ok) for listening for success events.
......
......@@ -7,7 +7,7 @@ var chai = require('chai')
chai.Assertion.includeStack = true
describe(Support.getTestDialectTeaser("CustomEventEmitter"), function () {
describe.only(Support.getTestDialectTeaser("CustomEventEmitter"), function () {
describe("proxy", function () {
it("should correctly work with success listeners", function(done) {
var emitter = new CustomEventEmitter()
......@@ -60,4 +60,46 @@ describe(Support.getTestDialectTeaser("CustomEventEmitter"), function () {
proxy.emit('success')
})
})
describe("when emitting an error event with an array of errors", function() {
describe("if no error handler is given", function() {
it("should throw the first error", function(done) {
var emitter = new CustomEventEmitter()
expect(function () {
emitter.emit("error", [
[
new Error("First error"),
new Error("Second error")
], [
new Error("Third error")
]
])
}).to.throw("First error")
done()
})
})
describe("if an error handler is given", function() {
it("should return the whole array", function(done) {
var emitter = new CustomEventEmitter()
var errors = [
[
new Error("First error"),
new Error("Second error")
], [
new Error("Third error")
]
]
emitter.error(function (err) {
expect(err).to.equal(errors)
done()
})
emitter.emit("error", errors)
})
})
})
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!