Skip to content

Commit

Permalink
Changed error handling logic to stop after error has been handled (#497)
Browse files Browse the repository at this point in the history
* Changed error handling logic to stop after error has been handled
  • Loading branch information
Philip Niedertscheider committed Apr 14, 2020
1 parent 2931123 commit c6c80a9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/__tests__/middy.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ describe('馃浀 Middy test suite', () => {
})
})

test('If theres an error and one error middleware handles the error, the next error middlewares is executed', (endTest) => {
test('If theres is an error and one error middleware handles the error, the next error middlewares should not be executed', (endTest) => {
const expectedResponse = { message: 'error handled' }

const onErrorMiddleware1 = jest.fn((handler, next) => {
Expand All @@ -297,7 +297,7 @@ describe('馃浀 Middy test suite', () => {
handler({}, {}, (err, response) => {
expect(err).toBeNull()
expect(onErrorMiddleware1).toBeCalled()
expect(onErrorMiddleware2).toBeCalled()
expect(onErrorMiddleware2).not.toHaveBeenCalled()
expect(response).toBe(expectedResponse)

endTest()
Expand Down
10 changes: 8 additions & 2 deletions src/middy.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,14 @@ const runErrorMiddlewares = (middlewares, instance, done) => {

const nextMiddleware = stack.shift()

if (nextMiddleware) {
const retVal = nextMiddleware(instance, runNext)
if (nextMiddleware && !instance.__handledError) {
const retVal = nextMiddleware(instance, (retCallBackError) => {
if (!retCallBackError) {
done()
} else {
runNext(retCallBackError)
}
})

if (retVal) {
if (!isPromise(retVal)) {
Expand Down

0 comments on commit c6c80a9

Please sign in to comment.