Option to disable automatic error capturing
try { throw new Error('Method not allowed'); }catch(err) { apm.captureError(err); res.send(500).json({message: err.message}); }
Let's assume a scenario as above. In this scenario, there will be two errors logged in to the Elastic Discover.
- Error captured automatically by apm agent
- Error captured by
apm.captureError()method
In the Discover, those two errors will be logged as below.

As displayed in this screenshot, there will be two errors logged. Among those two, one has the error message but other one doesn't.
I think it would be better if there is an option to disable this automatic error capturing feature. If we capture the error using apm.captureError()method, there should be only one error logged in the Discovery.
Hi @Dulajdeshan
Do you have a more complete code example that we could use to dig into that "1. Error captured automatically by apm agent" error? Also, could you paste the full Elasticsearch document for that error (use the > icon in the row in Discover to reveal that)? The other error fields might give us a clue about why there is no error.exception.message.
The logUncaughtExceptions: true configuration option will tell the APM agent to also log an uncaught exception when it is automatically capturing those. I find this helpful to get more detail on the console.
For example, so far I have:
const apm = require('elastic-apm-node').start({
logUncaughtExceptions: true
})
try {
throw new Error('Method not allowed')
} catch (err) {
apm.captureError(err)
res.send(500).json({ message: err.message })
}
which results in:
% node issue-2658.js
ReferenceError: res is not defined
at Object.<anonymous> (/Users/trentm/el/apm-agent-nodejs/issue-2658.js:9:3)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
at internal/main/run_main_module.js:17:47
{"log.level":"info","@timestamp":"2022-04-26T15:47:19.225Z","log":{"logger":"elastic-apm-node"},"ecs":{"version":"1.6.0"},"message":"Sending error to Elastic APM: {\"id\":\"e1755b93f58f42c3afe33199c3dd71fa\"}"}
{"log.level":"info","@timestamp":"2022-04-26T15:47:19.226Z","log":{"logger":"elastic-apm-node"},"ecs":{"version":"1.6.0"},"message":"Sending error to Elastic APM: {\"id\":\"18bf043bf084e9f98d7b1c29c889a360\"}"}
Obviously that is failing on res.send because I don't have a complete code example.
I think it would be better if there is an option to disable this automatic error capturing feature.
There is the captureExceptions APM agent config option that will turn off error capture for uncaughtExceptionss. I am not sure if this is what you are looking for, however.
@trentm Our application is a nodejs application. So res would be the response method of an express server. We have tried those config options in the elastic documentation. But it didn't work for us. Still automatic error capturing is happening. And one other thing, when we capture the error using apm.captureError() method, transaction name is not displayed in the Elastic Discover.
@Dulajdeshan Are you able to make a small code example that demonstrates the issue?
I will post a demo code for this ASAP.