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

Commit c95ef8ea by Mick Hansen

Merge pull request #1240 from saschagehlich/feature/customeventemitter-throw-error

Fix CustomEventEmitter error handling
2 parents 0c6a96a0 c75b9399
...@@ -28,6 +28,25 @@ module.exports = (function() { ...@@ -28,6 +28,25 @@ module.exports = (function() {
return this 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. Shortcut methods (success, ok) for listening for success events.
......
...@@ -60,4 +60,46 @@ describe(Support.getTestDialectTeaser("CustomEventEmitter"), function () { ...@@ -60,4 +60,46 @@ describe(Support.getTestDialectTeaser("CustomEventEmitter"), function () {
proxy.emit('success') 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!