@middy/core
Advanced tools
Comparing version 1.0.0-alpha.26 to 1.0.0-alpha.27
@@ -522,2 +522,51 @@ const middy = require('../') | ||
test('It should be possible to await a middyfied handler', async (endTest) => { | ||
const originalHandler = async (event, context) => Promise.resolve({ some: 'response' }) | ||
const handler = middy(originalHandler) | ||
const response = await handler({}, {}) | ||
expect(response).toEqual({ some: 'response' }) | ||
endTest() | ||
}) | ||
test('It should be possible to catch a middyfied handler rejection', async (endTest) => { | ||
const originalHandler = async (event, context) => Promise.reject(new Error('some error')) | ||
const handler = middy(originalHandler) | ||
try { | ||
await handler({}, {}) | ||
} catch (e) { | ||
expect(e.message).toEqual('some error') | ||
endTest() | ||
} | ||
}) | ||
test('Error from async handler with no callback is thrown up', async (endTest) => { | ||
const originalHandler = async (event, context) => { throw new Error('some error') } | ||
const handler = middy(originalHandler) | ||
try { | ||
await handler({}, {}) | ||
} catch (e) { | ||
expect(e.message).toEqual('some error') | ||
endTest() | ||
} | ||
}) | ||
test('Error from async handler is consumed by onError middleware', async (endTest) => { | ||
const handler = middy(async (event, context) => { | ||
throw new Error('some error') | ||
}) | ||
let onErrorWasCalled = false | ||
handler.use({ onError: (handler, next) => { | ||
onErrorWasCalled = true | ||
next() | ||
} }) | ||
await handler({}, {}) | ||
expect(onErrorWasCalled).toBe(true) | ||
endTest() | ||
}) | ||
test('A handler that returns a rejected promise will behave as an errored execution', (endTest) => { | ||
@@ -524,0 +573,0 @@ const handler = middy((event, context) => { |
71
index.js
@@ -158,48 +158,51 @@ const isPromise = require('./isPromise') | ||
const terminate = (err) => { | ||
if (err) { | ||
return callback(err) | ||
const middyPromise = new Promise((resolve, reject) => { | ||
const terminate = (err) => { | ||
if (err) { | ||
return callback ? callback(err) : reject(err) | ||
} | ||
return callback ? callback(null, instance.response) : resolve(instance.response) | ||
} | ||
return callback(null, instance.response) | ||
} | ||
const errorHandler = err => { | ||
instance.error = err | ||
return runErrorMiddlewares(errorMiddlewares, instance, terminate) | ||
} | ||
const errorHandler = err => { | ||
instance.error = err | ||
return runErrorMiddlewares(errorMiddlewares, instance, terminate) | ||
} | ||
runMiddlewares(beforeMiddlewares, instance, (err) => { | ||
if (err) return errorHandler(err) | ||
runMiddlewares(beforeMiddlewares, instance, (err) => { | ||
if (err) return errorHandler(err) | ||
const onHandlerError = once((err) => { | ||
instance.response = null | ||
errorHandler(err) | ||
}) | ||
const onHandlerError = once((err) => { | ||
instance.response = null | ||
return errorHandler(err) | ||
}) | ||
const onHandlerSuccess = once((response) => { | ||
instance.response = response | ||
runMiddlewares(afterMiddlewares, instance, (err) => { | ||
if (err) return errorHandler(err) | ||
const onHandlerSuccess = once((response) => { | ||
instance.response = response | ||
runMiddlewares(afterMiddlewares, instance, (err) => { | ||
if (err) return errorHandler(err) | ||
terminate() | ||
}) | ||
}) | ||
return terminate() | ||
const handlerReturnValue = handler.call(instance, instance.event, context, (err, response) => { | ||
if (err) return onHandlerError(err) | ||
onHandlerSuccess(response) | ||
}) | ||
}) | ||
const handlerReturnValue = handler.call(instance, instance.event, context, (err, response) => { | ||
if (err) return onHandlerError(err) | ||
onHandlerSuccess(response) | ||
}) | ||
// support for async/await promise return in handler | ||
if (handlerReturnValue) { | ||
if (!isPromise(handlerReturnValue)) { | ||
throw new Error('Unexpected return value in handler') | ||
} | ||
// support for async/await promise return in handler | ||
if (handlerReturnValue) { | ||
if (!isPromise(handlerReturnValue)) { | ||
throw new Error('Unexpected return value in handler') | ||
handlerReturnValue | ||
.then(onHandlerSuccess) | ||
.catch(onHandlerError) | ||
} | ||
handlerReturnValue | ||
.then(onHandlerSuccess) | ||
.catch(onHandlerError) | ||
} | ||
}) | ||
}) | ||
if (!instance.callback) return middyPromise | ||
} | ||
@@ -206,0 +209,0 @@ |
{ | ||
"name": "@middy/core", | ||
"version": "1.0.0-alpha.26", | ||
"version": "1.0.0-alpha.27", | ||
"description": "🛵 The stylish Node.js middleware engine for AWS Lambda (core package)", | ||
@@ -42,3 +42,3 @@ "engines": { | ||
}, | ||
"gitHead": "aa21b8f8cd8caa01b9eae49efc8b9d9e9b755799" | ||
"gitHead": "0da121c430a512efda580304c7ef3afff76ae2f2" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35507
890