Initial Setup with Jest & TypeScript [JavaScript]


Sentry is Error Tracking Service which provides cloud service and OSS. This post describes how to setup Sentry(Raven) with TypeScript/Node.js.

🎃 Install libraries

yarn add raven dotenv

# For development
yarn add --dev @types/raven @types/dotenv

🏀 Configure Sentry

Provide SENTRY_DSN environment variable to your application. In this case, we will provide the env variable by .env file with dotenv.

SENTRY_DSN='{PROTOCOL}://{PUBLIC_KEY}@{HOST}/{PATH}{PROJECT_ID}'

Create src/utils/Config.ts file to load environment variable.

import * as dotenv from "dotenv";
dotenv.config();
const ENV = process.env;

export const ENVIRONMENT = ENV.ENVIRONMENT;
export const SENTRY_DSN = ENV.SENTRY_DSN;

Create src/utils/Sentry.ts file for initializing Sentry.

import * as Raven from "raven";
import { ENVIRONMENT, SENTRY_DSN } from "./Config";

export async function setUpSentry() {
if (ENVIRONMENT !== "development") {
Raven.config(SENTRY_DSN, { captureUnhandledRejections: true }).install();
}
}

🎳 Activate Sentry in your code

Please write the following code to fist position of src/index.js (src/main.js) entry file.

setUpSentry().then(() => console.log('Initialize Sentry'));

🐠 Report error manually

Raven.config().install() method sets up global handler to automatically capture any uncatch exception. You can also report errors manually.

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

🐮 Report message

Raven.captureMessage('Broken!');

🐝 Add Context

You can also add context data to the Sentry error report. It supports your tracing a error.

Raven.context(function () {
Raven.setContext({
user: {
email: 'matt@example.com',
id: '123'
}
});
});

🗻 Express.js Configuration

If you want to set up Sentry with Express.js/TypeScript, pease configure as follows:

export default new App();

import * as express from "express";
import * as Raven from "raven";

const app = express();

// Sentry Configuration: Before doing anything else with it
setUpSentry().then(() => console.log("Set up Sentry"));

// Sentry request handler: Must be the first middleware on the app
this.app.use(Raven.requestHandler());

app.get("/", function mainHandler(req, res) {
throw new Error("Broke!");
});

// Sentry error handler: Must be before any other error middleware
app.use(Raven.errorHandler());

// Optional fallthrough error handler
app.use((err, req, res, next) => {
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
res.end(res.sentry + "\n");
});

app.listen(3000);

🤔 More information

If you want to know more information, please see https://docs.sentry.io/clients/node/ .

🚜 References

🖥 Recommended VPS Service

VULTR provides high performance cloud compute environment for you. Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour). In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!