Hello Team, I have a question regarding handling e...
# support
p
Hello Team, I have a question regarding handling exceptions in JavaScript for my Express application. I'm encountering difficulties retrieving exception data. While I can successfully handle exceptions manually as described in a blog post I found, it doesn't seem to work with automatic instrumentation. Is there something I might be overlooking? Blog: https://signoz.io/docs/userguide/exceptions/ Code:
Copy code
const express = require('express');
const app = express();

function divide(a, b) {
    if (b === 0) {
        throw new Error('Division by zero error');
    }
    return a / b;
}

app.get('/', (req, res) => {
    try {
        const result = divide(10, 0);
        console.log('Result:', result);
    } catch (error) {
        console.error('An error occurred:', error.message);
        res.status(400).send('Bad Request: ' + error.message);
        return;
    }
    res.send("Here is results of highest computed maths");
});

const port = 5000;
app.listen(port, () => {
    console.log(`Server is running on port ${port}`);
});
s
You are catching the exception yourself. The auto instrumentation wouldn't know about it. The exception must be thrown in the context of span (whether it's manual or auto-instrumented span) for it to be captured.
p
Thanks for the reply. This got resolved in CNCF slack channel.