|
| 1 | +import { HttpErrorResponse } from '@angular/common/http'; |
| 2 | + |
| 3 | +import type { ErrorEvent, EventHint } from '@sentry/angular'; |
| 4 | + |
| 5 | +export const SENTRY_IGNORE_ERRORS: (string | RegExp)[] = [ |
| 6 | + 'Handled unknown error', |
| 7 | + 'Non-Error promise rejection captured', |
| 8 | + 'no elements in sequence', |
| 9 | + /ResizeObserver loop/, |
| 10 | + 'error loading dynamically imported module', |
| 11 | + 'Importing a module script failed', |
| 12 | + 'Failed to fetch', |
| 13 | + 'Load failed', |
| 14 | + 'NetworkError when attempting to fetch resource', |
| 15 | + 'AbortError', |
| 16 | + 'The operation was aborted', |
| 17 | + 'The user aborted a request', |
| 18 | + 'ChunkLoadError', |
| 19 | + /Loading chunk [\w.-]+ failed/, |
| 20 | + 'Beacon is not defined', |
| 21 | +]; |
| 22 | + |
| 23 | +export const SENTRY_DENY_URLS: (string | RegExp)[] = [ |
| 24 | + /extensions\//i, |
| 25 | + /^chrome:\/\//i, |
| 26 | + /^chrome-extension:\/\//i, |
| 27 | + /^moz-extension:\/\//i, |
| 28 | + /^safari-extension:\/\//i, |
| 29 | + /^safari-web-extension:\/\//i, |
| 30 | + /^ms-browser-extension:\/\//i, |
| 31 | +]; |
| 32 | + |
| 33 | +const MIN_REPORTED_STATUS = 500; |
| 34 | +const MAX_UNWRAP_DEPTH = 4; |
| 35 | + |
| 36 | +const STATUS_MESSAGE_PATTERNS = [ |
| 37 | + /Http failure response for .*: (\d{1,3})(?:\s|$)/, |
| 38 | + /Server returned code (\d{1,3})(?:\s|$)/, |
| 39 | +]; |
| 40 | + |
| 41 | +const CAPTURED_OBJECT_KEYS = /(?:Object captured as exception|Non-Error exception captured) with keys: (.+)/; |
| 42 | + |
| 43 | +const WRAPPER_KEYS = ['ngOriginalError', 'rejection', 'cause'] as const; |
| 44 | +const HTTP_RESPONSE_KEYS = ['url', 'statusText', 'headers', 'ok'] as const; |
| 45 | + |
| 46 | +function isHttpResponseLike(value: object): value is { status: number } { |
| 47 | + const hasNumericStatus = 'status' in value && typeof (value as { status: unknown }).status === 'number'; |
| 48 | + |
| 49 | + return hasNumericStatus && HTTP_RESPONSE_KEYS.some((key) => key in value); |
| 50 | +} |
| 51 | + |
| 52 | +function describesHttpResponse(message: string | undefined): boolean { |
| 53 | + const keys = message |
| 54 | + ?.match(CAPTURED_OBJECT_KEYS)?.[1] |
| 55 | + .split(',') |
| 56 | + .map((key) => key.trim()); |
| 57 | + |
| 58 | + if (!keys?.includes('status')) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + return HTTP_RESPONSE_KEYS.some((key) => keys.includes(key)); |
| 63 | +} |
| 64 | + |
| 65 | +function getStatusFromMessage(message: string | undefined): number | null { |
| 66 | + if (!message) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + for (const pattern of STATUS_MESSAGE_PATTERNS) { |
| 71 | + const match = message.match(pattern); |
| 72 | + |
| 73 | + if (match) { |
| 74 | + return Number(match[1]); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return null; |
| 79 | +} |
| 80 | + |
| 81 | +function getStatusFromError(error: unknown, depth = 0): number | null { |
| 82 | + if (error instanceof HttpErrorResponse) { |
| 83 | + return error.status; |
| 84 | + } |
| 85 | + |
| 86 | + if (typeof error === 'string') { |
| 87 | + return getStatusFromMessage(error); |
| 88 | + } |
| 89 | + |
| 90 | + if (!error || typeof error !== 'object') { |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + if (isHttpResponseLike(error)) { |
| 95 | + return error.status; |
| 96 | + } |
| 97 | + |
| 98 | + if (depth >= MAX_UNWRAP_DEPTH) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + for (const key of WRAPPER_KEYS) { |
| 103 | + const status = getStatusFromError((error as Record<string, unknown>)[key], depth + 1); |
| 104 | + |
| 105 | + if (status !== null) { |
| 106 | + return status; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return null; |
| 111 | +} |
| 112 | + |
| 113 | +function getErrorMessage(error: unknown, event: ErrorEvent): string | undefined { |
| 114 | + if (typeof error === 'string') { |
| 115 | + return error; |
| 116 | + } |
| 117 | + |
| 118 | + if (error && typeof error === 'object' && 'message' in error && typeof error.message === 'string') { |
| 119 | + return error.message; |
| 120 | + } |
| 121 | + |
| 122 | + const values = event.exception?.values; |
| 123 | + |
| 124 | + return values?.[values.length - 1]?.value; |
| 125 | +} |
| 126 | + |
| 127 | +function getStatusFromSerialized(event: ErrorEvent, message: string | undefined): number | null { |
| 128 | + const serialized = event.extra?.['__serialized__']; |
| 129 | + |
| 130 | + if (!serialized || typeof serialized !== 'object') { |
| 131 | + return null; |
| 132 | + } |
| 133 | + |
| 134 | + const status = 'status' in serialized ? serialized.status : null; |
| 135 | + |
| 136 | + if (typeof status === 'number' && (isHttpResponseLike(serialized) || describesHttpResponse(message))) { |
| 137 | + return status; |
| 138 | + } |
| 139 | + |
| 140 | + if ('message' in serialized && typeof serialized.message === 'string') { |
| 141 | + return getStatusFromMessage(serialized.message); |
| 142 | + } |
| 143 | + |
| 144 | + return null; |
| 145 | +} |
| 146 | + |
| 147 | +function resolveHttpStatus(error: unknown, event: ErrorEvent): number | null { |
| 148 | + const message = getErrorMessage(error, event); |
| 149 | + |
| 150 | + return getStatusFromError(error) ?? getStatusFromSerialized(event, message) ?? getStatusFromMessage(message); |
| 151 | +} |
| 152 | + |
| 153 | +export function sentryBeforeSend(event: ErrorEvent, hint: EventHint): ErrorEvent | null { |
| 154 | + const status = resolveHttpStatus(hint.originalException, event); |
| 155 | + const isReportable = status === null || status >= MIN_REPORTED_STATUS; |
| 156 | + |
| 157 | + return isReportable ? event : null; |
| 158 | +} |
0 commit comments