Skip to content

Commit e1e1094

Browse files
committed
Fix incorrect handling of errors in plugins
1 parent e179854 commit e1e1094

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

src/utils/compose-middleware.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ export default function composeMiddleware (middleware) {
3434
return Promise.resolve()
3535
}
3636

37-
try {
38-
return Promise.resolve(fn(...args, () => dispatch(i + 1)))
39-
} catch (err) {
40-
return Promise.reject(err)
41-
}
37+
return new Promise((resolve, reject) => {
38+
fn(...args, (err) => {
39+
if (err) {
40+
return reject(err)
41+
}
42+
43+
return resolve(dispatch(i + 1))
44+
})
45+
})
4246
}
4347

4448
return dispatch(0)

test/unit/handlers/api-gateway.spec.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-unused-expressions */
22
import { should, expect } from 'chai'
3-
import { ApiGateway } from '../../../src/index.js'
3+
import { ApiGateway, AbstractLambdaPlugin, Enums } from '../../../src/index.js'
44

55
describe('api-gateway decorator', () => {
66
before(() => {
@@ -52,5 +52,39 @@ describe('api-gateway decorator', () => {
5252
done()
5353
})
5454
})
55+
56+
it('should pass an error throw when thrown in a PRE_REQUEST middleware', (done) => {
57+
class TestPlugin extends AbstractLambdaPlugin {
58+
constructor () {
59+
super('testPlugin', Enums.LambdaType.API_GATEWAY)
60+
61+
this.addHook(Enums.Hooks.PRE_EXECUTE, this.throwsError.bind(this))
62+
}
63+
64+
throwsError () {
65+
return (a, b, c, d, done) => {
66+
throw new Error('plugin-error')
67+
}
68+
}
69+
}
70+
71+
ApiGateway.registerPlugin(new TestPlugin())
72+
73+
class Test {
74+
@ApiGateway()
75+
testMethod (event) {
76+
console.log('should not execute')
77+
return { hello: 'world' }
78+
}
79+
}
80+
81+
const test = new Test()
82+
83+
test.testMethod({ test: 'test string' }, {}, (err, res) => {
84+
expect(err).to.be.null()
85+
res.statusCode.should.equal(500)
86+
done()
87+
})
88+
})
5589
})
5690
})

0 commit comments

Comments
 (0)