@sanity/sdk 3.12.0 → 3.13.0-next.20261106104949
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_exports/_internal.d.ts +11 -0
- package/dist/_exports/_internal.d.ts.map +1 -1
- package/dist/_exports/_internal.js +271 -6
- package/dist/_exports/_internal.js.map +1 -1
- package/dist/index.js +6 -515
- package/dist/index.js.map +1 -1
- package/dist/sentryBrowser-DT6hL7NZ.js +1 -0
- package/dist/version-DRnj2zr5.js +1072 -0
- package/dist/version-DRnj2zr5.js.map +1 -0
- package/package.json +3 -2
- package/src/_exports/_internal.ts +1 -0
- package/src/errorReporting/errorReporter.test.ts +207 -0
- package/src/errorReporting/errorReporter.ts +223 -0
- package/src/errorReporting/isErrorReportingEnabled.test.ts +67 -0
- package/src/errorReporting/isErrorReportingEnabled.ts +47 -0
- package/src/errorReporting/isKnownSdkError.test.ts +57 -0
- package/src/errorReporting/isKnownSdkError.ts +67 -0
- package/src/errorReporting/privacy.test.ts +67 -0
- package/src/errorReporting/privacy.ts +61 -0
- package/src/errorReporting/sentryBrowser.ts +10 -0
- package/src/errorReporting/sentryTypes.ts +7 -0
- package/dist/version-DfqE7Tzw.js +0 -559
- package/dist/version-DfqE7Tzw.js.map +0 -1
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import {CORE_SDK_VERSION} from '../version'
|
|
2
|
+
import {hasDebugFlag, isErrorReportingEnabled} from './isErrorReportingEnabled'
|
|
3
|
+
import {isKnownSdkError} from './isKnownSdkError'
|
|
4
|
+
import {beforeSend, sdkSentryDataCollection} from './privacy'
|
|
5
|
+
import {type BrowserOptions, type ErrorEvent, type Event, type Scope} from './sentryTypes'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The DSN of the SDK project in Sentry. It is a public ingest key, not a secret.
|
|
9
|
+
*/
|
|
10
|
+
const SDK_DSN = 'https://08e08ab0cbf33a49f7f5066597d5e374@sentry.sanity.io/4512347487666176'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Events go through the Sanity intake tunnel, which forwards them to Sentry. This avoids ad
|
|
14
|
+
* blockers and customer CSP `connect-src` rules that block the Sentry ingest host.
|
|
15
|
+
*/
|
|
16
|
+
const SDK_TUNNEL = 'https://api.sanity.io/vX/intake/tracing'
|
|
17
|
+
|
|
18
|
+
/** Most events a single page load may send. A render loop can throw thousands of errors. */
|
|
19
|
+
const MAX_EVENTS_PER_SESSION = 10
|
|
20
|
+
|
|
21
|
+
/** Events with the same message inside this window are reported once. */
|
|
22
|
+
const DUPLICATE_WINDOW_MS = 5 * 60 * 1000
|
|
23
|
+
|
|
24
|
+
/** Failed loads of the Sentry chunk allowed before the reporter gives up for the page load. */
|
|
25
|
+
const MAX_LOAD_FAILURES = 3
|
|
26
|
+
|
|
27
|
+
/** @internal */
|
|
28
|
+
export interface ReportErrorInfo {
|
|
29
|
+
/** The name of the error boundary that caught the error. */
|
|
30
|
+
errorBoundary?: string
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @internal */
|
|
34
|
+
export interface ErrorReporterOptions {
|
|
35
|
+
/** Replaces the fetch transport, for tests. */
|
|
36
|
+
transport?: BrowserOptions['transport']
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The value of the error that was thrown. When `linkedErrors` adds the causes, they come first
|
|
41
|
+
* and the thrown error is last.
|
|
42
|
+
*/
|
|
43
|
+
function getThrownValue(event: Event): string | undefined {
|
|
44
|
+
return event.exception?.values?.at(-1)?.value ?? event.message
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Caps the events per session and drops repeats of the same message inside a time window.
|
|
49
|
+
*/
|
|
50
|
+
function limitEventsIntegration() {
|
|
51
|
+
const seen = new Map<string, number>()
|
|
52
|
+
let sent = 0
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
name: 'SdkLimitEvents',
|
|
56
|
+
processEvent(event: Event): Event | null {
|
|
57
|
+
if (sent >= MAX_EVENTS_PER_SESSION) return null
|
|
58
|
+
|
|
59
|
+
const message = getThrownValue(event) ?? ''
|
|
60
|
+
const now = Date.now()
|
|
61
|
+
const previous = seen.get(message)
|
|
62
|
+
if (previous !== undefined && now - previous < DUPLICATE_WINDOW_MS) return null
|
|
63
|
+
|
|
64
|
+
seen.set(message, now)
|
|
65
|
+
sent++
|
|
66
|
+
return event
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Matches the SDK bundle served by the module server (`@sanity__sdk`, `@sanity__sdk-react`) and
|
|
73
|
+
* the same packages under `node_modules` (`@sanity/sdk`, or `@sanity_sdk-react` in a Vite
|
|
74
|
+
* dependency cache). The package name must end at a path separator or a file extension, so
|
|
75
|
+
* `@sanity/sdk-other` does not match.
|
|
76
|
+
*/
|
|
77
|
+
const SDK_MODULE_PATTERN = /(?:@|%40)sanity(?:__|_|\/|%2F)sdk(?:-react)?(?=[/.]|$)/
|
|
78
|
+
|
|
79
|
+
/** This repo's own sources, which only show up in local development with the debug flag set. */
|
|
80
|
+
const SDK_SOURCE_PATTERN = /\/packages\/(?:core|react)\/(?:src|dist)\//
|
|
81
|
+
|
|
82
|
+
/** Frame names the engine uses for code without a URL, such as `JSON.parse (<anonymous>)`. */
|
|
83
|
+
const BUILTIN_FRAME_PATTERN = /^(?:<.*>|native)$/
|
|
84
|
+
|
|
85
|
+
function isSdkFilename(filename: string): boolean {
|
|
86
|
+
return SDK_MODULE_PATTERN.test(filename) || (hasDebugFlag() && SDK_SOURCE_PATTERN.test(filename))
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The file of the innermost frame that has a URL. Builtins such as `JSON.parse` and `new URL`
|
|
91
|
+
* sit on top of the stack when they throw, and the code that called them decides the owner.
|
|
92
|
+
*/
|
|
93
|
+
function getThrowingFilename(event: Event): string | undefined {
|
|
94
|
+
const frames = event.exception?.values?.at(-1)?.stacktrace?.frames ?? []
|
|
95
|
+
for (let index = frames.length - 1; index >= 0; index--) {
|
|
96
|
+
const filename = frames[index]?.filename
|
|
97
|
+
if (filename && !BUILTIN_FRAME_PATTERN.test(filename)) return filename
|
|
98
|
+
}
|
|
99
|
+
return undefined
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Keeps only events thrown by SDK code. Error boundaries also catch errors thrown by the app
|
|
104
|
+
* rendered beneath them, so the frame that threw decides who owns the error. Events with no
|
|
105
|
+
* usable frame cannot be attributed and are dropped.
|
|
106
|
+
*/
|
|
107
|
+
function sdkOriginIntegration() {
|
|
108
|
+
return {
|
|
109
|
+
name: 'SdkOrigin',
|
|
110
|
+
processEvent(event: Event): Event | null {
|
|
111
|
+
const filename = getThrowingFilename(event)
|
|
112
|
+
return filename && isSdkFilename(filename) ? event : null
|
|
113
|
+
},
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Sentry marks every error it is given as already captured, and skips marked errors in every
|
|
119
|
+
* Sentry client on the page. Capturing a copy keeps that mark off the app's own error objects.
|
|
120
|
+
*/
|
|
121
|
+
function toCapturable(error: Error): Error {
|
|
122
|
+
const copy = new Error(
|
|
123
|
+
error.message,
|
|
124
|
+
error.cause === undefined ? undefined : {cause: error.cause},
|
|
125
|
+
)
|
|
126
|
+
copy.name = error.name
|
|
127
|
+
copy.stack = error.stack
|
|
128
|
+
return copy
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function getEnvironment(): string {
|
|
132
|
+
const hostname = typeof window === 'undefined' ? '' : window.location?.hostname
|
|
133
|
+
return hostname === 'localhost' || hostname === '127.0.0.1' ? 'development' : 'production'
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Creates a reporter that sends unexpected SDK errors to Sentry. It owns its own Sentry client
|
|
138
|
+
* and scope, and installs no global error handlers. Nothing is sent unless `reportError` is
|
|
139
|
+
* called.
|
|
140
|
+
*
|
|
141
|
+
* @internal
|
|
142
|
+
*/
|
|
143
|
+
export function createErrorReporter(options: ErrorReporterOptions = {}): {
|
|
144
|
+
reportError: (error: unknown, info?: ReportErrorInfo) => void
|
|
145
|
+
} {
|
|
146
|
+
let scopePromise: Promise<Scope> | undefined
|
|
147
|
+
let loadFailures = 0
|
|
148
|
+
|
|
149
|
+
function getScope(): Promise<Scope> {
|
|
150
|
+
scopePromise ??= import('./sentryBrowser')
|
|
151
|
+
.then((sentry) => {
|
|
152
|
+
const client = new sentry.BrowserClient({
|
|
153
|
+
dsn: SDK_DSN,
|
|
154
|
+
tunnel: SDK_TUNNEL,
|
|
155
|
+
release: String(CORE_SDK_VERSION),
|
|
156
|
+
environment: getEnvironment(),
|
|
157
|
+
dataCollection: sdkSentryDataCollection,
|
|
158
|
+
stackParser: sentry.defaultStackParser,
|
|
159
|
+
transport: options.transport ?? sentry.makeFetchTransport,
|
|
160
|
+
integrations: [
|
|
161
|
+
sentry.linkedErrorsIntegration(),
|
|
162
|
+
sdkOriginIntegration(),
|
|
163
|
+
limitEventsIntegration(),
|
|
164
|
+
],
|
|
165
|
+
beforeSend: (event: ErrorEvent) => beforeSend(event),
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
const scope = new sentry.Scope()
|
|
169
|
+
scope.setClient(client)
|
|
170
|
+
client.init()
|
|
171
|
+
return scope
|
|
172
|
+
})
|
|
173
|
+
.catch((error: unknown) => {
|
|
174
|
+
// Let the next report try again, for a chunk that failed to load while offline
|
|
175
|
+
scopePromise = undefined
|
|
176
|
+
loadFailures++
|
|
177
|
+
throw error
|
|
178
|
+
})
|
|
179
|
+
return scopePromise
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function reportError(error: unknown, info: ReportErrorInfo = {}): void {
|
|
183
|
+
// This runs inside `componentDidCatch`, where a throw would reach the parent boundary
|
|
184
|
+
try {
|
|
185
|
+
// Sentry builds a synthetic stack for non-Error values, and that stack points into this
|
|
186
|
+
// module, so such values would be attributed to the SDK
|
|
187
|
+
if (
|
|
188
|
+
!(error instanceof Error) ||
|
|
189
|
+
loadFailures >= MAX_LOAD_FAILURES ||
|
|
190
|
+
!isErrorReportingEnabled() ||
|
|
191
|
+
isKnownSdkError(error)
|
|
192
|
+
) {
|
|
193
|
+
return
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const capturable = toCapturable(error)
|
|
197
|
+
const {errorBoundary} = info
|
|
198
|
+
const tags = errorBoundary ? {handled: 'no', errorBoundary} : {handled: 'no'}
|
|
199
|
+
|
|
200
|
+
getScope()
|
|
201
|
+
.then((scope) => {
|
|
202
|
+
scope.captureException(capturable, {
|
|
203
|
+
mechanism: {handled: false, type: 'generic'},
|
|
204
|
+
captureContext: {tags},
|
|
205
|
+
})
|
|
206
|
+
})
|
|
207
|
+
.catch(() => {
|
|
208
|
+
// Nothing to do. The reporter must not add errors to the app it reports for.
|
|
209
|
+
})
|
|
210
|
+
} catch {
|
|
211
|
+
// Same: never throw into the caller
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return {reportError}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Reports an unexpected error from the SDK to Sentry, when error reporting is enabled.
|
|
220
|
+
*
|
|
221
|
+
* @internal
|
|
222
|
+
*/
|
|
223
|
+
export const {reportError} = createErrorReporter()
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {afterEach, describe, expect, it, vi} from 'vitest'
|
|
2
|
+
|
|
3
|
+
import {isErrorReportingEnabled} from './isErrorReportingEnabled'
|
|
4
|
+
|
|
5
|
+
function stubBrowser({
|
|
6
|
+
importMap,
|
|
7
|
+
debugFlag = false,
|
|
8
|
+
}: {
|
|
9
|
+
importMap?: string
|
|
10
|
+
debugFlag?: boolean
|
|
11
|
+
} = {}) {
|
|
12
|
+
vi.stubGlobal('window', {})
|
|
13
|
+
vi.stubGlobal('document', {
|
|
14
|
+
querySelectorAll: () => (importMap === undefined ? [] : [{textContent: importMap}]),
|
|
15
|
+
})
|
|
16
|
+
vi.stubGlobal('localStorage', {getItem: () => (debugFlag ? '1' : null)})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe('isErrorReportingEnabled', () => {
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
vi.unstubAllGlobals()
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('is disabled outside a browser', () => {
|
|
25
|
+
expect(isErrorReportingEnabled()).toBe(false)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('is disabled without an import map or the debug flag', () => {
|
|
29
|
+
stubBrowser()
|
|
30
|
+
expect(isErrorReportingEnabled()).toBe(false)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('is enabled when the import map has an @sanity/sdk entry', () => {
|
|
34
|
+
stubBrowser({importMap: JSON.stringify({imports: {'@sanity/sdk': 'https://example.com/sdk'}})})
|
|
35
|
+
expect(isErrorReportingEnabled()).toBe(true)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('is disabled when the import map has no @sanity/sdk entry', () => {
|
|
39
|
+
stubBrowser({importMap: JSON.stringify({imports: {react: 'https://example.com/react'}})})
|
|
40
|
+
expect(isErrorReportingEnabled()).toBe(false)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('is disabled when the import map is not valid JSON', () => {
|
|
44
|
+
stubBrowser({importMap: '{not json'})
|
|
45
|
+
expect(isErrorReportingEnabled()).toBe(false)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('is disabled when the import map has no imports', () => {
|
|
49
|
+
stubBrowser({importMap: '{}'})
|
|
50
|
+
expect(isErrorReportingEnabled()).toBe(false)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('is enabled by the debug flag', () => {
|
|
54
|
+
stubBrowser({debugFlag: true})
|
|
55
|
+
expect(isErrorReportingEnabled()).toBe(true)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('is disabled when localStorage throws', () => {
|
|
59
|
+
stubBrowser()
|
|
60
|
+
vi.stubGlobal('localStorage', {
|
|
61
|
+
getItem: () => {
|
|
62
|
+
throw new Error('blocked')
|
|
63
|
+
},
|
|
64
|
+
})
|
|
65
|
+
expect(isErrorReportingEnabled()).toBe(false)
|
|
66
|
+
})
|
|
67
|
+
})
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* localStorage key that forces error reporting on, for debugging the reporter itself.
|
|
3
|
+
*/
|
|
4
|
+
const DEBUG_KEY = 'SANITY_DEBUG_ERROR_REPORTING'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Whether the document has an import map with an `@sanity/sdk` entry. The Sanity CLI adds one
|
|
8
|
+
* when an app is deployed with `autoUpdates`, so its presence means Sanity serves the SDK bundle.
|
|
9
|
+
*/
|
|
10
|
+
function hasSdkPackageInImportMap(): boolean {
|
|
11
|
+
if (typeof document === 'undefined' || !('querySelectorAll' in document)) return false
|
|
12
|
+
|
|
13
|
+
return Array.from(document.querySelectorAll('script[type="importmap"]')).some((entry) => {
|
|
14
|
+
if (!entry.textContent) return false
|
|
15
|
+
try {
|
|
16
|
+
const imports = JSON.parse(entry.textContent).imports
|
|
17
|
+
return Boolean(imports) && '@sanity/sdk' in imports
|
|
18
|
+
} catch {
|
|
19
|
+
return false
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Whether the debug flag is set in localStorage. It also lets the origin filter accept this
|
|
26
|
+
* repo's own source paths, which only appear in local development.
|
|
27
|
+
*
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
export function hasDebugFlag(): boolean {
|
|
31
|
+
try {
|
|
32
|
+
return typeof localStorage !== 'undefined' && Boolean(localStorage.getItem(DEBUG_KEY))
|
|
33
|
+
} catch {
|
|
34
|
+
return false
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Error reporting only runs in apps where Sanity serves the SDK bundle (auto-updating apps), or
|
|
40
|
+
* when the debug flag is set. It never runs outside a browser.
|
|
41
|
+
*
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
export function isErrorReportingEnabled(): boolean {
|
|
45
|
+
if (typeof window === 'undefined') return false
|
|
46
|
+
return hasDebugFlag() || hasSdkPackageInImportMap()
|
|
47
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {ClientError, ConnectionFailedError, ServerError} from '@sanity/client'
|
|
2
|
+
import {describe, expect, it} from 'vitest'
|
|
3
|
+
|
|
4
|
+
import {MessageBusError} from '../dashboard/messageBus/bus'
|
|
5
|
+
import {TopicError} from '../dashboard/messageBus/topicStore'
|
|
6
|
+
import {DocumentInaccessibleError} from '../document/documentInaccessibleError'
|
|
7
|
+
import {ActionError} from '../document/processActions/shared'
|
|
8
|
+
import {isKnownSdkError} from './isKnownSdkError'
|
|
9
|
+
|
|
10
|
+
const response = (statusCode: number) =>
|
|
11
|
+
({statusCode, headers: {}, body: {}, url: 'https://example.com'}) as never
|
|
12
|
+
|
|
13
|
+
describe('isKnownSdkError', () => {
|
|
14
|
+
it.each([
|
|
15
|
+
['ActionError', new ActionError({message: 'm', documentId: 'd', transactionId: 't'})],
|
|
16
|
+
['DocumentInaccessibleError', new DocumentInaccessibleError('abc')],
|
|
17
|
+
['MessageBusError', new MessageBusError('timeout' as never)],
|
|
18
|
+
['TopicError', new TopicError('topic' as never)],
|
|
19
|
+
['ClientError', new ClientError(response(403))],
|
|
20
|
+
['ServerError', new ServerError(response(500))],
|
|
21
|
+
])('treats %s as known', (_name, error) => {
|
|
22
|
+
expect(isKnownSdkError(error)).toBe(true)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it.each([
|
|
26
|
+
['ConnectionFailedError', new ConnectionFailedError('connection failed')],
|
|
27
|
+
['a timeout error', Object.assign(new Error('timed out'), {name: 'TimeoutError'})],
|
|
28
|
+
['an AbortError', Object.assign(new Error('aborted'), {name: 'AbortError'})],
|
|
29
|
+
['an abort DOMException', new DOMException('The operation was aborted.', 'AbortError')],
|
|
30
|
+
['a failed fetch in Chromium', new TypeError('Failed to fetch')],
|
|
31
|
+
['a failed fetch in Firefox', new TypeError('NetworkError when attempting to fetch resource.')],
|
|
32
|
+
['a failed fetch in Safari', new TypeError('Load failed')],
|
|
33
|
+
])('treats %s as known', (_name, error) => {
|
|
34
|
+
expect(isKnownSdkError(error)).toBe(true)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('does not treat other TypeErrors as network failures', () => {
|
|
38
|
+
expect(isKnownSdkError(new TypeError('Cannot read properties of undefined'))).toBe(false)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('treats an error wrapping a known error as known', () => {
|
|
42
|
+
const wrapper = new Error('wrapper', {cause: new DocumentInaccessibleError('abc')})
|
|
43
|
+
expect(isKnownSdkError(wrapper)).toBe(true)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('treats unexpected errors as unknown', () => {
|
|
47
|
+
expect(isKnownSdkError(new TypeError('x is not a function'))).toBe(false)
|
|
48
|
+
expect(isKnownSdkError(new Error('boom'))).toBe(false)
|
|
49
|
+
expect(isKnownSdkError('boom')).toBe(false)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('stops on a cause chain that loops back on itself', () => {
|
|
53
|
+
const error = new Error('loop')
|
|
54
|
+
error.cause = error
|
|
55
|
+
expect(isKnownSdkError(error)).toBe(false)
|
|
56
|
+
})
|
|
57
|
+
})
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {ClientError, ConnectionFailedError, isTimeoutError, ServerError} from '@sanity/client'
|
|
2
|
+
|
|
3
|
+
import {MessageBusError} from '../dashboard/messageBus/bus'
|
|
4
|
+
import {TopicError} from '../dashboard/messageBus/topicStore'
|
|
5
|
+
import {DocumentInaccessibleError} from '../document/documentInaccessibleError'
|
|
6
|
+
import {ActionError} from '../document/processActions/shared'
|
|
7
|
+
|
|
8
|
+
const MAX_CAUSE_DEPTH = 5
|
|
9
|
+
|
|
10
|
+
/** Browsers word a failed `fetch` differently, and all of them throw a plain `TypeError`. */
|
|
11
|
+
const NETWORK_FAILURE_MESSAGE = /failed to fetch|networkerror|load failed|network request failed/i
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Aborting a request on teardown is routine. Matched by name because `DOMException` and abort
|
|
15
|
+
* errors from other libraries do not share a class.
|
|
16
|
+
*/
|
|
17
|
+
function isAbortError(error: unknown): boolean {
|
|
18
|
+
return error instanceof Error && error.name === 'AbortError'
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function isNetworkFailure(error: unknown): boolean {
|
|
22
|
+
return error instanceof TypeError && NETWORK_FAILURE_MESSAGE.test(error.message)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Known errors that cannot be matched by class. */
|
|
26
|
+
const KNOWN_ERROR_CHECKS: ((error: unknown) => boolean)[] = [
|
|
27
|
+
isTimeoutError,
|
|
28
|
+
isAbortError,
|
|
29
|
+
isNetworkFailure,
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
function isKnownErrorInstance(error: unknown): boolean {
|
|
33
|
+
// Built per call so the classes are read after every module has finished loading
|
|
34
|
+
const knownClasses = [
|
|
35
|
+
ActionError,
|
|
36
|
+
DocumentInaccessibleError,
|
|
37
|
+
MessageBusError,
|
|
38
|
+
TopicError,
|
|
39
|
+
// 4xx and 5xx responses from the Sanity API reflect the app's permissions, queries, or API
|
|
40
|
+
// availability, not a defect in the SDK
|
|
41
|
+
ClientError,
|
|
42
|
+
ServerError,
|
|
43
|
+
// The user is offline
|
|
44
|
+
ConnectionFailedError,
|
|
45
|
+
]
|
|
46
|
+
return (
|
|
47
|
+
knownClasses.some((knownClass) => error instanceof knownClass) ||
|
|
48
|
+
KNOWN_ERROR_CHECKS.some((check) => check(error))
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Whether an error is an expected state caused by the app's configuration, the user's
|
|
54
|
+
* permissions, the user's network, or the Sanity API, and so is not worth reporting as an SDK
|
|
55
|
+
* bug. Wrapped errors are checked through their `cause` chain.
|
|
56
|
+
*
|
|
57
|
+
* @internal
|
|
58
|
+
*/
|
|
59
|
+
export function isKnownSdkError(error: unknown): boolean {
|
|
60
|
+
let current = error
|
|
61
|
+
for (let depth = 0; depth < MAX_CAUSE_DEPTH; depth++) {
|
|
62
|
+
if (isKnownErrorInstance(current)) return true
|
|
63
|
+
if (!(current instanceof Error) || current.cause === undefined) return false
|
|
64
|
+
current = current.cause
|
|
65
|
+
}
|
|
66
|
+
return false
|
|
67
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {describe, expect, it} from 'vitest'
|
|
2
|
+
|
|
3
|
+
import {beforeSend} from './privacy'
|
|
4
|
+
import {type ErrorEvent} from './sentryTypes'
|
|
5
|
+
|
|
6
|
+
function makeEvent(overrides: Partial<ErrorEvent> = {}): ErrorEvent {
|
|
7
|
+
return {
|
|
8
|
+
type: undefined,
|
|
9
|
+
event_id: 'abc123',
|
|
10
|
+
timestamp: 1234,
|
|
11
|
+
platform: 'javascript',
|
|
12
|
+
level: 'error',
|
|
13
|
+
release: '3.10.0',
|
|
14
|
+
environment: 'production',
|
|
15
|
+
sdk: {name: 'sentry.javascript.browser', version: '11.8.0'},
|
|
16
|
+
exception: {values: [{type: 'TypeError', value: 'x is not a function'}]},
|
|
17
|
+
tags: {handled: 'no', errorBoundary: 'AuthBoundary'},
|
|
18
|
+
contexts: {react: {componentStack: '\n at Foo'}},
|
|
19
|
+
user: {id: 'u1', email: 'a@b.c', ip_address: '1.2.3.4'},
|
|
20
|
+
request: {url: 'https://app.example.com/page?token=secret', headers: {cookie: 'x'}},
|
|
21
|
+
...overrides,
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe('beforeSend', () => {
|
|
26
|
+
it('keeps the error, its identifiers, and the allowed tags', () => {
|
|
27
|
+
const event = beforeSend(makeEvent())
|
|
28
|
+
expect(event).toMatchObject({
|
|
29
|
+
event_id: 'abc123',
|
|
30
|
+
timestamp: 1234,
|
|
31
|
+
platform: 'javascript',
|
|
32
|
+
level: 'error',
|
|
33
|
+
release: '3.10.0',
|
|
34
|
+
environment: 'production',
|
|
35
|
+
sdk: {name: 'sentry.javascript.browser', version: '11.8.0'},
|
|
36
|
+
exception: {values: [{type: 'TypeError', value: 'x is not a function'}]},
|
|
37
|
+
tags: {handled: 'no', errorBoundary: 'AuthBoundary'},
|
|
38
|
+
})
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('replaces the user with a zeroed IP and deletes the request', () => {
|
|
42
|
+
const event = beforeSend(makeEvent())
|
|
43
|
+
expect(event.user).toEqual({ip_address: '0.0.0.0'})
|
|
44
|
+
expect(event.request).toBeUndefined()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('drops state a host page Sentry adds through the shared scopes', () => {
|
|
48
|
+
const event = beforeSend(
|
|
49
|
+
makeEvent({
|
|
50
|
+
tags: {handled: 'no', customer_tag: 'secret-tenant', hostproc: 'ran'},
|
|
51
|
+
extra: {cart: 'items'},
|
|
52
|
+
breadcrumbs: [{message: 'clicked buy for customer X'}],
|
|
53
|
+
contexts: {react: {componentStack: '\n at Foo'}, device: {model: 'x'}, app: {name: 'y'}},
|
|
54
|
+
fingerprint: ['host-fingerprint'],
|
|
55
|
+
transaction: '/checkout',
|
|
56
|
+
server_name: 'host',
|
|
57
|
+
}),
|
|
58
|
+
)
|
|
59
|
+
expect(event.tags).toEqual({handled: 'no'})
|
|
60
|
+
expect(event.extra).toBeUndefined()
|
|
61
|
+
expect(event.breadcrumbs).toBeUndefined()
|
|
62
|
+
expect(event.contexts).toBeUndefined()
|
|
63
|
+
expect(event.fingerprint).toBeUndefined()
|
|
64
|
+
expect(event.transaction).toBeUndefined()
|
|
65
|
+
expect(event.server_name).toBeUndefined()
|
|
66
|
+
})
|
|
67
|
+
})
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import {type BrowserOptions, type ErrorEvent} from './sentryTypes'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Key fragments Sentry denies when `sendDefaultPii` is left unset. The Sentry v11 migration guide
|
|
5
|
+
* uses the same list for headers and query params.
|
|
6
|
+
*/
|
|
7
|
+
const PII_KEY_DENYLIST = ['forwarded', '-ip', 'remote-', 'via', '-user']
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Turns off every automatic data category that can carry user or customer data. Matches the
|
|
11
|
+
* posture Sanity Studio uses for its own Sentry client.
|
|
12
|
+
*
|
|
13
|
+
* `userInfo: false` also stops Relay from inferring the client IP.
|
|
14
|
+
*/
|
|
15
|
+
export const sdkSentryDataCollection = {
|
|
16
|
+
userInfo: false,
|
|
17
|
+
cookies: false,
|
|
18
|
+
httpHeaders: {
|
|
19
|
+
request: {deny: PII_KEY_DENYLIST},
|
|
20
|
+
response: {deny: PII_KEY_DENYLIST},
|
|
21
|
+
},
|
|
22
|
+
httpBodies: [],
|
|
23
|
+
urlQueryParams: {deny: PII_KEY_DENYLIST},
|
|
24
|
+
genAI: {inputs: false, outputs: false},
|
|
25
|
+
databaseQueryData: false,
|
|
26
|
+
graphQL: {document: false, variables: false},
|
|
27
|
+
queues: false,
|
|
28
|
+
} satisfies NonNullable<BrowserOptions['dataCollection']>
|
|
29
|
+
|
|
30
|
+
const KEPT_TAGS = ['handled', 'errorBoundary']
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Sentry `beforeSend` hook that rebuilds the event from an allowlist of fields. Sentry merges
|
|
34
|
+
* the global and isolation scopes into every event, and when the host page runs the same Sentry
|
|
35
|
+
* version those scopes are the host's: its tags, extras, breadcrumbs, and contexts would be
|
|
36
|
+
* sent along with the SDK's error. Anything not listed here is dropped, and the zeroed IP stops
|
|
37
|
+
* Sentry from filling one in from the connection. Exception messages and stack frames are kept
|
|
38
|
+
* as they are.
|
|
39
|
+
*/
|
|
40
|
+
export function beforeSend(event: ErrorEvent): ErrorEvent {
|
|
41
|
+
const tags: NonNullable<ErrorEvent['tags']> = {}
|
|
42
|
+
for (const key of KEPT_TAGS) {
|
|
43
|
+
const value = event.tags?.[key]
|
|
44
|
+
if (value !== undefined) tags[key] = value
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
type: undefined,
|
|
49
|
+
event_id: event.event_id,
|
|
50
|
+
timestamp: event.timestamp,
|
|
51
|
+
platform: event.platform,
|
|
52
|
+
level: event.level,
|
|
53
|
+
release: event.release,
|
|
54
|
+
environment: event.environment,
|
|
55
|
+
message: event.message,
|
|
56
|
+
exception: event.exception,
|
|
57
|
+
sdk: event.sdk,
|
|
58
|
+
tags,
|
|
59
|
+
user: {ip_address: '0.0.0.0'},
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Loaded with `import()` by the error reporter. Importing `@sentry/browser` directly would pull the
|
|
2
|
+
// whole package into every bundle that reaches the reporter, since a bundler cannot tree-shake the
|
|
3
|
+
// namespace of a dynamic import. Named re-exports keep only what the reporter uses.
|
|
4
|
+
export {
|
|
5
|
+
BrowserClient,
|
|
6
|
+
defaultStackParser,
|
|
7
|
+
linkedErrorsIntegration,
|
|
8
|
+
makeFetchTransport,
|
|
9
|
+
Scope,
|
|
10
|
+
} from '@sentry/browser'
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// These are `import()` type aliases on purpose. A type-only `import {type X} from '@sentry/browser'`
|
|
2
|
+
// still leaves a static import of the package in the bundle, and that stops the lazy
|
|
3
|
+
// `import('@sentry/browser')` in `errorReporter.ts` from becoming its own chunk.
|
|
4
|
+
export type BrowserOptions = import('@sentry/browser').BrowserOptions
|
|
5
|
+
export type ErrorEvent = import('@sentry/browser').ErrorEvent
|
|
6
|
+
export type Event = import('@sentry/browser').Event
|
|
7
|
+
export type Scope = import('@sentry/browser').Scope
|