[middy/core] get event input in timeoutEarlyResponse parameter
boringContributor opened this issue · comments
Is your feature request related to a problem? Please describe.
I'm using middy for a lambda task in my step function. I don't see a clear way to check if the lambda times out (e.g. for logging reasons). I stumbled across the timeoutEarlyResponse
option and feel like this fits my use case well. However, I don't have any information about the request there, which makes it hard to act upon it.
Describe the solution you'd like
export const handler = middy({
timeoutEarlyInMillis: 50,
timeoutEarlyResponse: (event) => {
logger.warn('event timed out', { event })
return {
statusCode: 408,
}
}
}).handler(lambdaHandler)
Describe alternatives you've considered
There are multiple other ways.
- use a
setTimeout
and check with thegetRemainingTimeInMillis
from the context - use a Promise.race with a timeout function and the actual task
However, it seems the easiest for me to just have the event in the timeoutEarlyResponse
method, because I currently don't see a drawback?
If you think this is a good idea, I'm more than happy to contribute it :)
By default Middy has timeoutEarly
enabled and will throw an error. If you take a look at @middy/core
you'll see we use a Promise.race (https://github.com/middyjs/middy/blob/main/packages/core/index.js#L167-L189). All you need to do to log the timeout error is to add a middleware that logs onError
. If you want to catch only timeouts, look for err.name === 'TimeoutError'
.