-
Notifications
You must be signed in to change notification settings - Fork 56
feat: diagnostics uncaught sdk errors installed by script #1419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Mercy811
merged 7 commits into
main
from
AMP-143673-diagnostics-uncaught-error-script-auto
Dec 9, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c7e06b1
feat: diagnostics uncaught error installed by script
Mercy811 00fef33
test: diagnostics uncaught error installed by script
Mercy811 4e19e79
test: test-server
Mercy811 2faf456
test: revert playground
Mercy811 24d0bde
feat: diagnostics uncaught error installed by script
Mercy811 439684b
fix: should return undefined if currentScript not work
Mercy811 7e791ce
fix: use URL for normalization
Mercy811 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
packages/analytics-core/src/diagnostics/uncaught-sdk-errors.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import { getGlobalScope } from '../global-scope'; | ||
| import { IDiagnosticsClient } from './diagnostics-client'; | ||
|
|
||
| type ErrorEventType = 'error' | 'unhandledrejection'; | ||
|
|
||
| interface CapturedErrorContext { | ||
| readonly type: ErrorEventType; | ||
| readonly message: string; | ||
| readonly stack?: string; | ||
| readonly filename?: string; | ||
| readonly errorName?: string; | ||
| readonly metadata?: Record<string, unknown>; | ||
| } | ||
|
|
||
| export const GLOBAL_KEY = '__AMPLITUDE_SCRIPT_URL__'; | ||
| export const EVENT_NAME_ERROR_UNCAUGHT = 'sdk.error.uncaught'; | ||
|
|
||
| const getNormalizedScriptUrl = (): string | undefined => { | ||
| const scope = getGlobalScope() as Record<string, unknown> | null; | ||
| /* istanbul ignore next */ | ||
| return scope?.[GLOBAL_KEY] as string | undefined; | ||
| }; | ||
|
|
||
| const setNormalizedScriptUrl = (url: string) => { | ||
| const scope = getGlobalScope() as Record<string, unknown> | null; | ||
| if (scope) { | ||
| scope[GLOBAL_KEY] = url; | ||
| } | ||
| }; | ||
|
|
||
| export const registerSdkLoaderMetadata = (metadata: { scriptUrl?: string }) => { | ||
| if (metadata.scriptUrl) { | ||
| const normalized = normalizeUrl(metadata.scriptUrl); | ||
| if (normalized) { | ||
| setNormalizedScriptUrl(normalized); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| export const enableSdkErrorListeners = (client: IDiagnosticsClient) => { | ||
| const scope = getGlobalScope(); | ||
|
|
||
| if (!scope || typeof scope.addEventListener !== 'function') { | ||
| return; | ||
| } | ||
|
|
||
| const handleError = (event: ErrorEvent) => { | ||
| const error = event.error instanceof Error ? event.error : undefined; | ||
| const stack = error?.stack; | ||
| const match = detectSdkOrigin({ filename: event.filename, stack }); | ||
| if (!match) { | ||
| return; | ||
| } | ||
|
|
||
| capture({ | ||
| type: 'error', | ||
| message: event.message, | ||
| stack, | ||
| filename: event.filename, | ||
| errorName: error?.name, | ||
| metadata: { | ||
| colno: event.colno, | ||
| lineno: event.lineno, | ||
| isTrusted: event.isTrusted, | ||
| matchReason: match, | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| const handleRejection = (event: PromiseRejectionEvent) => { | ||
| const error = event.reason instanceof Error ? event.reason : undefined; | ||
| const stack = error?.stack; | ||
| const filename = extractFilenameFromStack(stack); | ||
| const match = detectSdkOrigin({ filename, stack }); | ||
|
|
||
| if (!match) { | ||
| return; | ||
| } | ||
|
|
||
| /* istanbul ignore next */ | ||
| capture({ | ||
| type: 'unhandledrejection', | ||
| message: error?.message ?? stringifyReason(event.reason), | ||
| stack, | ||
| filename, | ||
| errorName: error?.name, | ||
| metadata: { | ||
| isTrusted: event.isTrusted, | ||
| matchReason: match, | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| const capture = (context: CapturedErrorContext) => { | ||
| client.recordEvent(EVENT_NAME_ERROR_UNCAUGHT, { | ||
| type: context.type, | ||
| message: context.message, | ||
| filename: context.filename, | ||
| error_name: context.errorName, | ||
| stack: context.stack, | ||
| ...context.metadata, | ||
| }); | ||
| }; | ||
|
|
||
| scope.addEventListener('error', handleError, true); | ||
| scope.addEventListener('unhandledrejection', handleRejection, true); | ||
| }; | ||
Mercy811 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const detectSdkOrigin = (payload: { filename?: string; stack?: string }): 'filename' | 'stack' | undefined => { | ||
| const normalizedScriptUrl = getNormalizedScriptUrl(); | ||
| if (!normalizedScriptUrl) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (payload.filename && payload.filename.includes(normalizedScriptUrl)) { | ||
| return 'filename'; | ||
| } | ||
|
|
||
| if (payload.stack && payload.stack.includes(normalizedScriptUrl)) { | ||
Mercy811 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return 'stack'; | ||
| } | ||
|
|
||
| return undefined; | ||
| }; | ||
|
|
||
| const normalizeUrl = (value: string) => { | ||
| try { | ||
| /* istanbul ignore next */ | ||
| const url = new URL(value, getGlobalScope()?.location?.origin); | ||
| return url.origin + url.pathname; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| }; | ||
|
|
||
| const extractFilenameFromStack = (stack?: string) => { | ||
| if (!stack) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const match = stack.match(/(https?:\/\/\S+?)(?=[)\s]|$)/); | ||
| /* istanbul ignore next */ | ||
| return match ? match[1] : undefined; | ||
| }; | ||
|
|
||
| /* istanbul ignore next */ | ||
| const stringifyReason = (reason: unknown) => { | ||
| if (typeof reason === 'string') { | ||
| return reason; | ||
| } | ||
|
|
||
| try { | ||
| return JSON.stringify(reason); | ||
| } catch { | ||
| return '[object Object]'; | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.