From e05a41ec00b4e947f8d3a41a6020c16949efd86b Mon Sep 17 00:00:00 2001 From: Loren Segal Date: Mon, 25 Mar 2013 16:46:06 -0700 Subject: [PATCH] Propagate errors from all callbacks up to main runloop Fixes #74 --- lib/request.js | 7 +++---- lib/sequential_executor.js | 23 ++++++++++++++--------- test/event_listeners.spec.coffee | 5 ++--- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/request.js b/lib/request.js index 2f49b39343..ce9acccebd 100644 --- a/lib/request.js +++ b/lib/request.js @@ -336,10 +336,10 @@ AWS.Request = inherit({ }, /** - * @private + * @api private */ emitEvents: function emitEvents(eventNames, response, doneCallback) { - if (!doneCallback) doneCallback = function() {}; + if (!doneCallback) doneCallback = this.unhandledErrorCallback; if (response.error) { doneCallback.call(this, response.error); } else if (eventNames.length === 0) { @@ -362,8 +362,7 @@ AWS.Request = inherit({ * @api private */ emitEvent: function emitEvent(eventName, args, doneCallback) { - if (!doneCallback) doneCallback = function() {}; - + if (!doneCallback) doneCallback = this.unhandledErrorCallback; var response = null; if (AWS.util.isType(args, Array)) { response = args[args.length - 1]; diff --git a/lib/sequential_executor.js b/lib/sequential_executor.js index 96cb36a324..f0e53f7691 100644 --- a/lib/sequential_executor.js +++ b/lib/sequential_executor.js @@ -85,15 +85,12 @@ AWS.SequentialExecutor = AWS.util.inherit({ * @api private */ emit: function emit(eventName, eventArgs, doneCallback) { - if (!doneCallback) doneCallback = function() {}; + if (!doneCallback) doneCallback = this.unhandledErrorCallback; + var listeners = this.listeners(eventName); - if (listeners.length === 0) { - doneCallback.call(this); - return false; - } else { - this.callListeners(listeners, eventArgs, doneCallback); - return true; - } + var count = listeners.length; + this.callListeners(listeners, eventArgs, doneCallback); + return count > 0; }, /** @@ -234,8 +231,16 @@ AWS.SequentialExecutor = AWS.util.inherit({ } ); return this; - } + }, + /** + * @api private + */ + unhandledErrorCallback: function unhandledErrorCallback(err) { + if (err) { + throw err; + } + } }); /** diff --git a/test/event_listeners.spec.coffee b/test/event_listeners.spec.coffee index 38a21b89eb..ee3d893f9e 100644 --- a/test/event_listeners.spec.coffee +++ b/test/event_listeners.spec.coffee @@ -316,6 +316,5 @@ describe 'AWS.EventListeners', -> request = makeRequest() request.on 'error', -> throw "ERROR" - response = request.send() - expect(completeHandler).toHaveBeenCalled() - expect(response.error).toBe("ERROR") + expect(-> request.send()).toThrow('ERROR') + expect(completeHandler).not.toHaveBeenCalled()