I'm using serverless framework to deploy lamdbas containing bugsnag.
I'm using aws-serverless-express to have express running.
I had to write my own middleware to ensure the bugsnag request is fulfilled before finishing the lambda
From this file https://github.com/bugsnag/bugsnag-js/blob/next/packages/plugin-express/src/express.js#L49
I replaced this
const errorHandler = (err, req, res, next) => {
if (req.bugsnag) {
req.bugsnag.notify(createReportFromErr(err, handledState))
} else {
client._logger.warn(
'req.bugsnag is not defined. Make sure the @bugsnag/plugin-express requestHandler middleware is added first.'
)
client.notify(createReportFromErr(err, handledState, getRequestAndMetaDataFromReq(req)))
}
next(err)
}
by
app.use(async (err, req, res, next) => {
await new Promise(resolve => {
req.bugsnag.notify(createReportFromErr(err, handledState), {}, resolve)
})
next(err)
})
that way it ensures the request has been fulfilled.
Could this become an option?
It might make sense for us to make this change (which is equivalent to your change but inkeeping with the current async style in that file):
const errorHandler = (err, req, res, next) => {
if (req.bugsnag) {
- req.bugsnag.notify(createReportFromErr(err, handledState))
+ req.bugsnag.notify(createReportFromErr(err, handledState), {}, () => next(err))
} else {
client._logger.warn(
'req.bugsnag is not defined. Make sure the @bugsnag/plugin-express requestHandler middleware is added first.'
)
- client.notify(createReportFromErr(err, handledState, getRequestAndMetaDataFromReq(req)))
+ client.notify(createReportFromErr(err, handledState, getRequestAndMetaDataFromReq(req)), {}, () => {
+ next(err)
+ })
}
- next(err)
}
But I'm surprised that the lamdba will terminate whilst there is network or file i/o going on, so would need to investigate.
Are you sure your (or the built aws-serverless-express) error middleware doesn't forcibly do a process.exit(n)?
@bengourley confirming @tanguyantoine issue. If you do not await the bugsnag.notify it will not send.
I want to add that to make the notify call work in a lambda you could also add asynchronous=False to the bugsnag configuration otherwise the lambda will terminate before the notification has been send.
Is there any update to this?
I tried adding asynchronous: false to my Bugsnag config but it didn't seem to work. I'm still having an issue with being able to log errors from my lambdas.
Hi @destinyb-realityi
AWS Lamdba is not something which we officially support yet, but it may be worth trying something like this:
await new Promise(resolve => (bugsnagClient.notify(err), resolve));
Hi,
I was having this same issue, except that my use case is not AWS lambda but simply a short-lived process that runs in an end user local desktop environment. (but it's similar in the way that both "app instances" run quickly and then shut down)
The suggestion by @phillipsam worked for me. I've implemented an utility function like this:
const bugsnagNotifyAsync = (
error: NotifiableError,
onError?: OnErrorCallback
) =>
new Promise((resolve, reject) =>
Bugsnag.notify(error, onError, (err, res) =>
err ? reject(err) : resolve(res)
)
);
then in my error handler, I do:
await bugsnagNotifyAsync(error);
However, I must also say that in my current environment:
Win10
Node v10.16.0
@bugsnag/js@^7.0.1
I've measured this call and it's taking about 1500~6000ms to complete, which seems to be quite long. Is this expected behavior?
Hey @tmilar, and for future readers of this issue. The await new Promise method previously suggested in this thread is likely not suitable for your needs, as it does not guarantee that the Lambda, or Node.js script will not exit before fulfilling all promises. Apologies for the wrong steer.
Can we have an official solution for this? More than a year with no actual solution
Yes, what the...
I just started testing out Bugsnag on my Netlify Functions.
Worked fine locally, did not work live.
Had to do this to get it to report live:
...
catch (error) {
console.warn(error)
Bugsnag.notify(error)
await wait(300)
return {
...
}
}
Seems really flaky :man_shrugging:
The new AWS Extensions API might offer good ways to solve this issue: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html
Most helpful comment
Can we have an official solution for this? More than a year with no actual solution