Sumurai8
Sumurai8

Reputation: 20737

Sentry error logging with express does not send any errors

When I am trying to send errors to Sentry for a simple node.js Express server, I don't get any errors logged in Sentry itself. There are no inbound filters set up and it does not report anything filtered in the past 30 days. Allowed domains is set to *, which is I think the default. The code that I use is more or less the same as the example code in the documentation. However, when calling the endpoint nothing appears in Sentry and the debug lines do not show anything about an error even being sent.

How do I get Sentry to properly capture errors?

This is test.js

'use strict';

const express = require('express');
const Sentry = require('@sentry/node');

const app = express();

// TODO dotenv setting
const SENTRY_NODE_DSN = process.env.SENTRY_NODE_DSN || 'the ingest url from settings > client keys';

console.log(`Started logging errors at sentry on DSN ${SENTRY_NODE_DSN}`);
// Sentry
Sentry.init({
    dsn: SENTRY_NODE_DSN,
    debug: true,
});

// The request handler must be the first middleware on the app
app.use(Sentry.Handlers.requestHandler());

const port = process.argv[2];
if (port === undefined) {
    console.log(`Please specify a port as first argument`);
    process.exit(0);
}

app.get('/test', () => {
    throw new Error('test');
});

// The error handler must be before any other error middleware and after all controllers
app.use(Sentry.Handlers.errorHandler({
    shouldHandleError(error) {
        return true;
    }
}));

app.listen(port);

We start it with node ./test.js 3000

Then from a different window we execute wget -- localhost:3000/test which gives the following output.

--2021-07-29 14:03:01--  http://localhost:3000/test
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:3000... connected.
HTTP request sent, awaiting response... 500 Internal Server Error
2021-07-29 14:03:01 ERROR 500: Internal Server Error.

The output from the express server is this:

$ node ./test.js 3000
Started logging errors at sentry on DSN same url as in the code above
Sentry Logger [Log]: Integration installed: InboundFilters
Sentry Logger [Log]: Integration installed: FunctionToString
Sentry Logger [Log]: Integration installed: Console
Sentry Logger [Log]: Integration installed: Http
Sentry Logger [Log]: Integration installed: OnUncaughtException
Sentry Logger [Log]: Integration installed: OnUnhandledRejection
Sentry Logger [Log]: Integration installed: LinkedErrors
Error: test
    at /var/www/vhosts/myproject/test.js:28:8
    at Layer.handle [as handle_request] (/var/www/vhosts/myproject/node_modules/express/lib/router/layer.js:95:5)
    at next (/var/www/vhosts/myproject/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/var/www/vhosts/myproject/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/var/www/vhosts/myproject/node_modules/express/lib/router/layer.js:95:5)
    at /var/www/vhosts/myproject/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/var/www/vhosts/myproject/node_modules/express/lib/router/index.js:335:12)
    at next (/var/www/vhosts/myproject/node_modules/express/lib/router/index.js:275:10)
    at Domain.<anonymous> (/var/www/vhosts/myproject/node_modules/@sentry/node/dist/handlers.js:321:13)
    at Domain.run (domain.js:370:15)

Upvotes: 1

Views: 7778

Answers (1)

Sumurai8
Sumurai8

Reputation: 20737

In my case this code was correct, but the issue was that I was running this script in wsl while on a VPN, and the request was being sent, but hang. Experimentally it appears that Sentry does not log anything when trying to send errors when in debug mode, so you can't infer anything from that.

To test if Sentry does actually send anything, try the even simpler version

const Sentry = require('@sentry/node');
const SENTRY_NODE_DSN = 'the ingest url';
Sentry.init({
    dsn: SENTRY_NODE_DSN,
    debug: true,
});
Sentry.captureException(new Error('test exception'));

If the script hangs (does not terminate), it means that the http-request to the sentry servers is not being completed. This is likely due to a network issue on your side.

Upvotes: 6

Related Questions