Skip to content

Commit be8b634

Browse files
HazATjherr
authored andcommitted
Update Sentry example to show banner in case DSN isn't defined
1 parent 5b800d0 commit be8b634

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
lines changed
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import * as Sentry from '@sentry/tanstackstart-react'
2-
Sentry.init({
3-
dsn: import.meta.env.VITE_SENTRY_DSN,
4-
// Adds request headers and IP for users, for more info visit:
5-
// https://docs.sentry.io/platforms/javascript/guides/tanstackstart-react/configuration/options/#sendDefaultPii
6-
sendDefaultPii: true,
7-
tracesSampleRate: 1.0,
8-
replaysSessionSampleRate: 1.0,
9-
replaysOnErrorSampleRate: 1.0,
10-
})
2+
3+
const sentryDsn = import.meta.env?.VITE_SENTRY_DSN ?? process.env.VITE_SENTRY_DSN
4+
5+
if (!sentryDsn) {
6+
console.warn('VITE_SENTRY_DSN is not defined. Sentry is not running.')
7+
} else {
8+
Sentry.init({
9+
dsn: sentryDsn,
10+
// Adds request headers and IP for users, for more info visit:
11+
// https://docs.sentry.io/platforms/javascript/guides/tanstackstart-react/configuration/options/#sendDefaultPii
12+
sendDefaultPii: true,
13+
tracesSampleRate: 1.0,
14+
replaysSessionSampleRate: 1.0,
15+
replaysOnErrorSampleRate: 1.0,
16+
})
17+
}

frameworks/react-cra/add-ons/sentry/assets/src/routes/demo/sentry.testing.tsx

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,16 @@ function RouteComponent() {
285285
const [results, setResults] = useState<
286286
Record<string, { type: 'success' | 'error'; spanOp: string }>
287287
>({})
288+
const [sentryConfigured, setSentryConfigured] = useState<boolean | null>(null)
289+
290+
useEffect(() => {
291+
// Check if Sentry DSN environment variable is set
292+
const hasDsn = !!import.meta.env.VITE_SENTRY_DSN
293+
setSentryConfigured(hasDsn)
294+
}, [])
295+
296+
// Don't show warning until we've checked on the client
297+
const showWarning = sentryConfigured === false
288298

289299
const handleClientError = async () => {
290300
setIsLoading((prev) => ({ ...prev, clientError: true }))
@@ -394,8 +404,8 @@ function RouteComponent() {
394404
</div>
395405
</div>
396406
<p className="text-lg text-[#A49FB5] max-w-xl mx-auto leading-relaxed">
397-
Click the buttons below to trigger errors and traces, then view
398-
them in your{' '}
407+
Click the buttons below to trigger errors and traces, then view them
408+
in your{' '}
399409
<a
400410
href="https://sentry.io"
401411
target="_blank"
@@ -408,6 +418,32 @@ function RouteComponent() {
408418
</p>
409419
</div>
410420

421+
{/* Sentry Not Initialized Warning */}
422+
{showWarning && (
423+
<div className="mb-8 flex items-center gap-3 bg-[#E5A000]/10 border border-[#E5A000]/30 rounded-xl px-6 py-4">
424+
<svg
425+
className="w-6 h-6 text-[#E5A000] flex-shrink-0"
426+
fill="none"
427+
strokeLinecap="round"
428+
strokeLinejoin="round"
429+
strokeWidth="2"
430+
viewBox="0 0 24 24"
431+
stroke="currentColor"
432+
>
433+
<title>Warning</title>
434+
<path d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
435+
</svg>
436+
<div>
437+
<p className="text-[#E5A000] font-medium">
438+
Sentry is not initialized
439+
</p>
440+
<p className="text-[#A49FB5] text-sm mt-1">
441+
Set the <code className="bg-[#1C1825] px-1.5 py-0.5 rounded text-[#B3A1FF]">VITE_SENTRY_DSN</code> environment variable to enable error tracking and performance monitoring.
442+
</p>
443+
</div>
444+
</div>
445+
)}
446+
411447
{/* Features Grid */}
412448
<div className="grid grid-cols-1 md:grid-cols-4 gap-4 mb-12">
413449
<FeatureCard
@@ -463,6 +499,7 @@ function RouteComponent() {
463499
variant="error"
464500
onClick={handleClientError}
465501
loading={isLoading.clientError}
502+
disabled={sentryConfigured === false}
466503
>
467504
Trigger Client Error
468505
</SentryButton>
@@ -483,6 +520,7 @@ function RouteComponent() {
483520
variant="primary"
484521
onClick={handleClientTrace}
485522
loading={isLoading.clientTrace}
523+
disabled={sentryConfigured === false}
486524
>
487525
Test Client Trace
488526
</SentryButton>
@@ -513,6 +551,7 @@ function RouteComponent() {
513551
variant="error"
514552
onClick={handleServerError}
515553
loading={isLoading.serverError}
554+
disabled={sentryConfigured === false}
516555
>
517556
Trigger Server Error
518557
</SentryButton>
@@ -533,6 +572,7 @@ function RouteComponent() {
533572
variant="primary"
534573
onClick={handleServerTrace}
535574
loading={isLoading.serverTrace}
575+
disabled={sentryConfigured === false}
536576
>
537577
Test Server Trace
538578
</SentryButton>

0 commit comments

Comments
 (0)