(
originalOpen,
xhrOpenThisArg: XMLHttpRequest & SentryWrappedXMLHttpRequest,
xhrOpenArgArray:
| [method: string, url: string | URL]
| [method: string, url: string | URL, async: boolean, username?: string | null, password?: string | null],
)
| 31 | // eslint-disable-next-line @typescript-eslint/unbound-method |
| 32 | xhrproto.open = new Proxy(xhrproto.open, { |
| 33 | apply( |
| 34 | originalOpen, |
| 35 | xhrOpenThisArg: XMLHttpRequest & SentryWrappedXMLHttpRequest, |
| 36 | xhrOpenArgArray: |
| 37 | | [method: string, url: string | URL] |
| 38 | | [method: string, url: string | URL, async: boolean, username?: string | null, password?: string | null], |
| 39 | ) { |
| 40 | // NOTE: If you are a Sentry user, and you are seeing this stack frame, |
| 41 | // it means the error, that was caused by your XHR call did not |
| 42 | // have a stack trace. If you are using HttpClient integration, |
| 43 | // this is the expected behavior, as we are using this virtual error to capture |
| 44 | // the location of your XHR call, and group your HttpClient events accordingly. |
| 45 | const virtualError = new Error(); |
| 46 | |
| 47 | const startTimestamp = timestampInSeconds() * 1000; |
| 48 | |
| 49 | // open() should always be called with two or more arguments |
| 50 | // But to be on the safe side, we actually validate this and bail out if we don't have a method & url |
| 51 | const method = isString(xhrOpenArgArray[0]) ? xhrOpenArgArray[0].toUpperCase() : undefined; |
| 52 | const url = parseXhrUrlArg(xhrOpenArgArray[1]); |
| 53 | |
| 54 | if (!method || !url) { |
| 55 | return originalOpen.apply(xhrOpenThisArg, xhrOpenArgArray); |
| 56 | } |
| 57 | |
| 58 | xhrOpenThisArg[SENTRY_XHR_DATA_KEY] = { |
| 59 | method, |
| 60 | url, |
| 61 | request_headers: {}, |
| 62 | }; |
| 63 | |
| 64 | // if Sentry key appears in URL, don't capture it as a request |
| 65 | if (method === 'POST' && url.match(/sentry_key/)) { |
| 66 | xhrOpenThisArg.__sentry_own_request__ = true; |
| 67 | } |
| 68 | |
| 69 | const onreadystatechangeHandler: () => void = () => { |
| 70 | // For whatever reason, this is not the same instance here as from the outer method |
| 71 | const xhrInfo = xhrOpenThisArg[SENTRY_XHR_DATA_KEY]; |
| 72 | |
| 73 | if (!xhrInfo) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | if (xhrOpenThisArg.readyState === 4) { |
| 78 | try { |
| 79 | // touching statusCode in some platforms throws |
| 80 | // an exception |
| 81 | xhrInfo.status_code = xhrOpenThisArg.status; |
| 82 | } catch { |
| 83 | /* do nothing */ |
| 84 | } |
| 85 | |
| 86 | const handlerData: HandlerDataXhr = { |
| 87 | endTimestamp: timestampInSeconds() * 1000, |
| 88 | startTimestamp, |
| 89 | xhr: xhrOpenThisArg, |
| 90 | virtualError, |
nothing calls this directly
no test coverage detected