middyjs / middy

🛵 The stylish Node.js middleware engine for AWS Lambda 🛵

Home Page:https://middy.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

error-logger: Call implementation function with await prefix

Dzhuneyt opened this issue · comments

Is your feature request related to a problem? Please describe.
When using the error-logger package, it's very common to want to proxy these errors to third party services in an async way, e.g. call a webhook using axios or pass that error to Sentry. The current implementation, expects the client-provided error handling function to be synchronous, even though Middy supports async error handlers.

Describe the solution you'd like

Replace this line:

logger(request)

With:

await logger(request)

The surrounding function is already async, so this should not be a problem.

Additional context
The current alternative is to use something like this:

middy().use(errorLogger({
                logger: (request) => {
                    axios.post(url, request.error).then()
                },
            }))

But the provided logger function will not be await-ed and there's a possibility that the axios call will get cancelled if it's too slow. My suggested solution will cause the axios call to be completed, before moving on to the next error handler.

Thanks for reporting.await was purposely left off to allow the lambda to not have to wait before sending the response and the logs can be save in the background. Are you finding that your logs are not being sent or something?