Skip to content

Commit

Permalink
Log error and exit if exception is raised from a callback
Browse files Browse the repository at this point in the history
References #74
  • Loading branch information
lsegal committed Aug 12, 2013
1 parent 55b1c39 commit 3cdd424
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
12 changes: 6 additions & 6 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,19 +473,19 @@ AWS.Request = inherit({
if (retryError) this.failRequest(response);
});
} else {
this.emitEvent('success', [response], this.handledErrorCallback);
this.emitEvent('complete', [response], this.handledErrorCallback);
this.emitEvent('success', [response], this.unhandledErrorCallback);
this.emitEvent('complete', [response], this.unhandledErrorCallback);
}
});
},

/**
* @api private
*/
failRequest: function failRequest(response) {
this.emitEvent('error', [response.error, response], this.handledErrorCallback);
this.emitEvent('complete', [response], this.handledErrorCallback);
},
failRequest: function failRequest(response) {
this.emitEvent('error', [response.error, response], this.unhandledErrorCallback);
this.emitEvent('complete', [response], this.unhandledErrorCallback);
},

/**
* @api private
Expand Down
12 changes: 2 additions & 10 deletions lib/sequential_executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,18 +258,10 @@ AWS.SequentialExecutor = AWS.util.inherit({
err.domainThrown = false;
this.domain.emit('error', err);
} else {
throw err;
console.error(err.stack ? err.stack : err);
process.exit(1);
}
}
},

/**
* @api private
*/
handledErrorCallback: function handledErrorCallback(err) {
try {
this.unhandledErrorCallback(err);
} catch (e) { }
}
});

Expand Down
17 changes: 14 additions & 3 deletions test/event_listeners.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -330,29 +330,40 @@ describe 'AWS.EventListeners', ->
expect(completeHandler).toHaveBeenCalled()

describe 'terminal callback error handling', ->
beforeEach ->
spyOn(process, 'exit')
spyOn(console, 'error')

didError = ->
expect(console.error).toHaveBeenCalledWith('ERROR')
expect(process.exit).toHaveBeenCalledWith(1)

describe 'without domains', ->
it 'ignores exceptions raised from success event', ->
it 'logs exceptions raised from success event and exits process', ->
helpers.mockHttpResponse 200, {}, []
request = makeRequest()
request.on 'success', -> throw "ERROR"
expect(-> request.send()).not.toThrow('ERROR')
expect(completeHandler).toHaveBeenCalled()
expect(retryHandler).not.toHaveBeenCalled()
didError()

it 'ignores exceptions raised from complete event', ->
it 'logs exceptions raised from complete event and exits process', ->
helpers.mockHttpResponse 200, {}, []
request = makeRequest()
request.on 'complete', -> throw "ERROR"
expect(-> request.send()).not.toThrow('ERROR')
expect(completeHandler).toHaveBeenCalled()
expect(retryHandler).not.toHaveBeenCalled()
didError()

it 'ignores exceptions raised from error event', ->
it 'logs exceptions raised from error event and exits process', ->
helpers.mockHttpResponse 500, {}, []
request = makeRequest()
request.on 'error', -> throw "ERROR"
expect(-> request.send()).not.toThrow('ERROR')
expect(completeHandler).toHaveBeenCalled()
didError()

describe 'with domains', ->
it 'sends error raised from complete event to a domain', ->
Expand Down

0 comments on commit 3cdd424

Please sign in to comment.