forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentry.ts
More file actions
56 lines (50 loc) · 1.48 KB
/
Copy pathsentry.ts
File metadata and controls
56 lines (50 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use client'
/**
* Thin wrapper around Sentry so the rest of the codebase doesn't import
* @sentry/nextjs directly (keeps the dependency swappable and tree-shakeable).
*/
let _sentry: typeof import('@sentry/nextjs') | null = null
async function getSentry() {
if (_sentry) return _sentry
try {
_sentry = await import('@sentry/nextjs')
} catch {
_sentry = null
}
return _sentry
}
/** Report an error to Sentry with optional context tags. */
export async function captureError(
err: unknown,
context?: {
walletAddress?: string
operation?: string
extra?: Record<string, unknown>
},
): Promise<void> {
const sentry = await getSentry()
if (!sentry) return
sentry.withScope((scope) => {
if (context?.walletAddress) {
// Attach wallet public key as user id — never a private key.
scope.setUser({ id: context.walletAddress.slice(0, 8) + '…' })
}
if (context?.operation) {
scope.setTag('operation', context.operation)
}
if (context?.extra) {
Object.entries(context.extra).forEach(([k, v]) => scope.setExtra(k, v))
}
sentry.captureException(err)
})
}
/** Set the connected wallet address as Sentry user context. Call on wallet connect/disconnect. */
export async function setSentryUser(walletAddress: string | null): Promise<void> {
const sentry = await getSentry()
if (!sentry) return
if (walletAddress) {
sentry.setUser({ id: walletAddress.slice(0, 8) + '…' })
} else {
sentry.setUser(null)
}
}