Skip to content

Commit

Permalink
🔧 (sentry) add support for environment
Browse files Browse the repository at this point in the history
  • Loading branch information
mlbrgl authored and owidbot committed Jan 10, 2024
1 parent e1997e0 commit 7df1fcd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .dev.vars.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# env vars for local development of /functions/donation/donate.ts Cloudflare pages function
# rename to .dev.vars and fill in the values from the homonymous "(dev)" keys in 1Password

ENV=development

# https://dashboard.stripe.com/test/apikeys
# required by /donation/donate
STRIPE_SECRET_KEY=
Expand All @@ -15,5 +17,5 @@ STRIPE_WEBHOOK_SECRET=
MAILGUN_DOMAIN=
MAILGUN_SENDING_KEY=

#optional
# optional
SENTRY_DSN=
10 changes: 8 additions & 2 deletions functions/donation/_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import { CaptureConsole } from "@sentry/integrations"

interface SentryEnvVars {
SENTRY_DSN: string
ENV: "production" | "development"
}

const hasSentryEnvVars = (env: any): env is SentryEnvVars => {
return !!env.SENTRY_DSN
return (
!!env.SENTRY_DSN &&
!!env.ENV &&
["production", "development"].includes(env.ENV)
)
}

export const onRequest: PagesFunction = (context) => {
if (!hasSentryEnvVars(context.env)) {
console.error(
"Missing SENTRY_DSN environment variable. Continuing without error logging..."
"Missing Sentry environment variables. Continuing without error logging..."
)
// Gracefully continue if Sentry is not configured.
return context.next()
Expand All @@ -21,5 +26,6 @@ export const onRequest: PagesFunction = (context) => {
return sentryPlugin({
dsn: context.env.SENTRY_DSN,
integrations: [new CaptureConsole()],
environment: context.env.ENV,
})(context)
}

0 comments on commit 7df1fcd

Please sign in to comment.