From c4971ebabfd90d3dd86ed9210fe70329fdd6cce9 Mon Sep 17 00:00:00 2001 From: Hollis Wu Date: Fri, 16 Feb 2024 22:53:22 -0500 Subject: [PATCH] Add sentry error reporting --- package.json | 1 + pnpm-lock.yaml | 27 +++++++++++++++++++++++++++ src/index.ts | 15 +++++++++++++++ worker-configuration.d.ts | 5 +++++ 4 files changed, 48 insertions(+) diff --git a/package.json b/package.json index 17f3f8f..722cec9 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "wrangler": "^3.0.0" }, "dependencies": { + "@cfworker/sentry": "^2.0.0", "@octokit/auth-app": "^6.0.3", "octokit": "^3.1.2" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6e8bedd..8aa7af2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,6 +5,9 @@ settings: excludeLinksFromLockfile: false dependencies: + "@cfworker/sentry": + specifier: ^2.0.0 + version: 2.0.0 "@octokit/auth-app": specifier: ^6.0.3 version: 6.0.3 @@ -87,6 +90,23 @@ packages: js-tokens: 4.0.0 dev: true + /@cfworker/sentry@2.0.0: + resolution: + { + integrity: sha512-P8WbEkT9QvHS/3taqP0eEi8J+akAAOgyK/kWSxu1BE6+/2lwDgT94abB+vkd7uIpivJIfuBemw7S0OQsKHqcbA==, + } + dependencies: + "@cfworker/worker-types": 1.12.3 + "@cloudflare/workers-types": 2.2.2 + dev: false + + /@cfworker/worker-types@1.12.3: + resolution: + { + integrity: sha512-p9wWxB1n+D0Mm9YFYcU80Ea9rUqYDAkuYvSPjKezCSjFKGDm12x9S/8md33h7a4IAzxtO9pfOVoVLvVI2hpR9g==, + } + dev: false + /@cloudflare/kv-asset-handler@0.3.1: resolution: { @@ -156,6 +176,13 @@ packages: dev: true optional: true + /@cloudflare/workers-types@2.2.2: + resolution: + { + integrity: sha512-kaMn2rueJ0PL1TYVGknTCh0X0x0d9G+FNXAFep7/4uqecEZoQb/63o6rOmMuiqI09zLuHV6xhKRXinokV/MY9A==, + } + dev: false + /@cloudflare/workers-types@4.20240208.0: resolution: { diff --git a/src/index.ts b/src/index.ts index 6c2b2c4..3373f46 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import { type IRequestStrict, Router, error, text } from "itty-router"; +import { captureError } from "@cfworker/sentry"; import { useOctoApp } from "./octo"; import { unwrap } from "./utils"; @@ -39,6 +40,20 @@ export default { return await router.handle(request, env, ctx); } catch (error) { console.error("Router raised exception", error); + + if (env.SENTRY_DSN) { + const { posted } = captureError({ + sentryDsn: env.SENTRY_DSN, + environment: "prod", + release: "release", + err: error, + request, + user: "", + }); + + ctx.waitUntil(posted); + } + return text("Internal server error", { status: 500 }); } }, diff --git a/worker-configuration.d.ts b/worker-configuration.d.ts index 92d522b..792575c 100644 --- a/worker-configuration.d.ts +++ b/worker-configuration.d.ts @@ -14,6 +14,11 @@ interface Env { */ GH_PRIVATE_KEY: string; + /** + * Sentry DSN. Optional and might not be configured + */ + SENTRY_DSN?: string; + // Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/ // MY_KV_NAMESPACE: KVNamespace; //