How to use context to capture all errors in NodeJS

The documentation states the following:

The recommended usage pattern, though, is to run your entire program inside a Raven context:

var Raven = require('raven');

Raven.config('___').install();
Raven.context(function () {
  // all your stuff goes here
});

Unfortunately, I’m not entirely sure what to nest inside the context wrapper to capture all exceptions thrown by my REST API.

My SPA currently consumes my REST API and I want to capture all exceptions thrown by the backend and pass them to Sentry.

My server.js file contains the following code:

const listingRoutesApi = require('./api/routes/listings');
const userRoutesApi = require('./api/routes/users');

app.use('/api', listingRoutesApi);
app.use('/api', userRoutesApi);

My api folder structure is shown below:

api/
├── controllers/
│   ├── listings.js
│   ├── users.js
├── models/
│   ├── listings.js
│   └── users.js
└── routes/
│   ├── listings.js
│   └── users.js

My ./api/routes/listings.js file contains the following snippet.

const ctrlListings = require('../controllers/listings');
router.get("/listings", ctrlListings.listingsByDistance);

Essentially, I want to capture every exception thrown by every function within controllers but I’m not sure how to accomplish this by reading the documentation.

I’ve also already referred to theExpress documentation and this SO post; however, neither solution has worked for me (no errors are caught by Sentry). I can confirm that catching errors manually in the following way DOES work.

try {
    doSomething(a[0]);
} catch (e) {
    Raven.captureException(e);
}