Reporting Errors to Bug Tracking Service

I’ve battled with a good solution to this for a long time. Whilst many services offer a Node Express middleware tracking solution getting errors to report correctly is a challenge because of getting the errors to surface from Parse > Express > Node > Tracking (this is how it works in my head).

Since 3.x this has been a lot easier as to my mind there are actually some global express errors surfaced and therefore caught by the tracking software.

I am using Bugsnag (best price/quota and nice UI).

I’ve set this up as such:

/app/index.js

const bugsnag = require('@bugsnag/js');
const bugsnagExpress = require('@bugsnag/plugin-express');
const bugsnagClient = bugsnag({
  apiKey: process.env.BUGSNAG_API_KEY,
  appVersion: pkg.version,
  appType: 'api-server',
  notifyReleaseStages: ['development', 'production'],
  releaseStage: rgEnvironment,
});
bugsnagClient.use(bugsnagExpress);
const bugsnagMiddleware = bugsnagClient.getPlugin('express');
// COMPLETE BUGSNAG
  app.use(function (err, req, res, next) { // eslint-disable-line no-unused-vars
    const isDev = process.env.NODE_ENV === 'development' || process.env.RG_ENV === 'development';

    req.bugsnag.notify(extractError(err), {
      metaData: {
        version: pkg.version,
        serverId: process.env.SERVER_ID,
        node: process.version,
        err,
      },
    });

    res.status(err.status || 500);
    res.json({
      message: err.message,
      error: isDev
        ? err // development error handler will print stacktrace
        : {}, // production error handler, no stacktraces leaking
    });

    // Bugsnag would log this here, but we capture it above so we can add metadata
    // next(new Error(err.message));
  });

index.js (parse invocation)

// Final handler - Bugsnag. This handles any errors that Express catches
app.use(coreApi.bugsnagMiddleware.errorHandler);

This all works fine… however, when trying to use something like this in one of my modules I am unable to pass in the bugsnagClient

See here: https://docs.bugsnag.com/platforms/javascript/express/#reporting-handled-errors

This is what I would like to achieve. How can I get the bugsnagClient as a global throughout my parse cloud code?

Just to make sure that I understood what you need. Do you want to do something like req.bugsnag.notify(...); inside a cloud code function?

Exact that :point_up_2: @davimacedo

As we can see in this line of Parse Server, the original Express.js req is not passed to the cloud code functions. So I don’t think you will be able to do this in the current Parse Server implementation. I think you should open an issue for discussion in the Parse Server repo to possibly include the raw Express.js request object in the arguments when calling the cloud code functions.

Thanks @davimacedo, it’s possible to run another instance of Bugsnag within the cloud ode, I got that working in my testing, however it just feels a ‘little wrong’ to do that.

I’ll keep digging and open an issue if I don’t get anywhere.

1 Like