Context
I'm using elastic-apm-node for monitoring Next server, however, I can not capture the Uncaught exception on server (error happens when rendering component on server, not the error at getServerSideProps
)
ErrorBoundary approach will not work, because it captures the error at client.
Custom server also does not work
createServer(async (req, res) => {
try {
const parsedUrl = parse(req.url, true);
setTransactionName(req.url);
await handle(req, res, parsedUrl);
captureInternalError(res);
} catch (err) {
captureError(err);
res.statusCode = 500;
res.end('internal server error');
}
})
.once('error', (err) => {
captureError(err);
process.exit(1);
})
the try--catch
and .once('error'
do not be triggered when error happened at rendering component on server.
- Catch at
getInitialProps
of_error
page is what is needed, but having some problems- it can not run server code even I check to execute on server only
getInitialProps = async ({ res, err, locale }) => {
if(!!res) { // only execute on server
import('elastic-apm-node').then(({captureError}) => {
logger.error(error); // custom logger
captureError(err);
})
}
})
it throws error module fs
not found (fs was using by elastic-apm-node)
- Even we have
_error
, but every Uncaught exception happens, there is an error log stacktrade by Next with multiple lines which is not good for logging. (we have a custom logger to log 1 line already)
Questions
- is there a way to capture all errors on server
- can we disable auto log stacktrade for Uncaught exception?