From 1e277ec3ed5831f9be5ee42b86335420ac800d1c Mon Sep 17 00:00:00 2001 From: Loren Segal Date: Tue, 21 Oct 2014 19:37:43 -0700 Subject: [PATCH] Allow SDK errors to be thrown out of final request callback. Fixes #392, referencing #74. --- lib/request.js | 23 +++++++++-------------- lib/sequential_executor.js | 7 +------ test/event_listeners.spec.coffee | 4 ++-- test/helpers.coffee | 4 ++-- 4 files changed, 14 insertions(+), 24 deletions(-) diff --git a/lib/request.js b/lib/request.js index 4164222719..ec39ddd8e3 100644 --- a/lib/request.js +++ b/lib/request.js @@ -13,24 +13,19 @@ function isTerminalState(machine) { var fsm = new AcceptorStateMachine(); fsm.setupStates = function() { - var transition = function(err, done) { + var transition = function(_, done) { + var self = this; + try { - var self = this; self.emit(self._asm.currentState, function() { - var nextError = self.response.error; - if (nextError && nextError !== err && isTerminalState(self)) { - throw nextError; - } - - done(nextError); + done(self.response.error); }); - - } catch (e) { - if (e !== err && isTerminalState(self)) { - AWS.SequentialExecutor.prototype.unhandledErrorCallback.call(this, e); - done(); + } catch (err) { + if (isTerminalState(self)) { + AWS.SequentialExecutor.prototype.unhandledErrorCallback.call(self, err); } else { - done(e); + self.response.error = err; + done(err); } } }; diff --git a/lib/sequential_executor.js b/lib/sequential_executor.js index cf52c56d86..e71b654f1b 100644 --- a/lib/sequential_executor.js +++ b/lib/sequential_executor.js @@ -99,12 +99,7 @@ AWS.SequentialExecutor = AWS.util.inherit({ listener.apply(self, args.concat([callNextListener])); return; // stop here, callNextListener will continue } else { // synchronous listener - try { - listener.apply(self, args); - } catch (err) { - doneCallback.call(self, err); - return; - } + listener.apply(self, args); } } diff --git a/test/event_listeners.spec.coffee b/test/event_listeners.spec.coffee index 7b72af8cf7..d86fd70a95 100644 --- a/test/event_listeners.spec.coffee +++ b/test/event_listeners.spec.coffee @@ -434,9 +434,9 @@ describe 'AWS.EventListeners', -> describe 'terminal callback error handling', -> describe 'without domains', -> it 'emits uncaughtException', -> - helpers.mockHttpResponse 200, {}, [] + helpers.mockResponse data: {} expect(-> (makeRequest -> invalidCode)).to.throw() - expect(completeHandler.calls.length).not.to.equal(0) + expect(completeHandler.calls.length).to.equal(1) expect(errorHandler.calls.length).to.equal(0) expect(retryHandler.calls.length).to.equal(0) diff --git a/test/helpers.coffee b/test/helpers.coffee index 361268a2ca..6be2acd89c 100644 --- a/test/helpers.coffee +++ b/test/helpers.coffee @@ -90,10 +90,10 @@ MockService = AWS.Service.defineService 'mockService', @config.region = 'mock-region' setupRequestListeners: (request) -> request.on 'extractData', (resp) -> - resp.data = resp.httpResponse.body.toString() + resp.data = (resp.httpResponse.body||'').toString() request.on 'extractError', (resp) -> resp.error = - code: resp.httpResponse.body.toString() || resp.httpResponse.statusCode + code: (resp.httpResponse.body||'').toString() || resp.httpResponse.statusCode message: null api: new AWS.Model.Api metadata: endpointPrefix: 'mockservice'