Skip to content

Commit 5223b07

Browse files
feat: add sentry error logging
1 parent 7676e89 commit 5223b07

File tree

5 files changed

+219
-5
lines changed

5 files changed

+219
-5
lines changed

.env.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ METRICS_AUTH_TOKEN="A_SECRET_TOKEN
2424
2525
AVATAR_URL="https://cdn.discordapp.com/avatars/760778370119761921/b09d7fd2ec0f27e29d000f4fd62d8ea5.webp"
2626

27-
NO_MIGRATION_AFTER=1649923675917
27+
NO_MIGRATION_AFTER=1649923675917
28+
29+
SENTRY_DSN="dsn"

package-lock.json

Lines changed: 193 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"dependencies": {
2222
"@prisma/client": "^3.9.2",
23+
"@sentry/node": "^6.19.6",
2324
"@sinclair/typebox": "^0.20.5",
2425
"axios": "^0.26.0",
2526
"detritus-client-rest": "^0.10.5",

src/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import metricsPlugin from "./plugins/metrics";
1313
import webhookAndLoggingPlugin from "./plugins/logging";
1414
import fastifyCookie, { FastifyCookieOptions } from "fastify-cookie";
1515
import interactionsPlugin from "./interactions/index";
16+
import Sentry from "@sentry/node";
1617

1718
import authRoutePlugin from "./authRoutes";
1819
const instance: FastifyInstance = fastify({
@@ -21,6 +22,22 @@ const instance: FastifyInstance = fastify({
2122

2223
await instance.register(envPlugin); // Load env variables
2324

25+
// Sentry is registered before all other plugins incase they throw errors
26+
// Sentry is not a plugin so all errors are captured
27+
28+
Sentry.init({
29+
dsn: instance.envVars.SENTRY_DSN,
30+
});
31+
32+
instance.setErrorHandler(async (error, request, reply) => {
33+
Sentry.captureException(error);
34+
instance.log.error(error);
35+
return reply.status(500).send({
36+
statusCode: 500,
37+
error: "Internal Server Error",
38+
message: error.message,
39+
});
40+
});
2441
// These are plugins that are separate from versioning
2542
await instance.register(prismaPlugin);
2643
await instance.register(discordRestPlugin, {

src/plugins/envCheck.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const schemaForEnv = {
2222
"PORT",
2323
"HOST",
2424
"NO_MIGRATION_AFTER",
25+
"SENTRY_DSN",
2526
],
2627
properties: {
2728
UUID_NAMESPACE: {
@@ -76,6 +77,9 @@ const schemaForEnv = {
7677
NO_MIGRATION_AFTER: {
7778
type: "number",
7879
},
80+
SENTRY_DSN: {
81+
type: "string",
82+
},
7983
},
8084
};
8185

@@ -97,6 +101,7 @@ interface EnvVars {
97101
HOST: string;
98102
PRISMA_FIELD_ENCRYPTION_KEY: string;
99103
NO_MIGRATION_AFTER: number;
104+
SENTRY_DSN: string;
100105
}
101106

102107
declare module "fastify" {

0 commit comments

Comments
 (0)