( stackParser: StackParser, exception: unknown, syntheticException?: Error, attachStacktrace?: boolean, isUnhandledRejection?: boolean, )
| 264 | * @hidden |
| 265 | */ |
| 266 | export function eventFromUnknownInput( |
| 267 | stackParser: StackParser, |
| 268 | exception: unknown, |
| 269 | syntheticException?: Error, |
| 270 | attachStacktrace?: boolean, |
| 271 | isUnhandledRejection?: boolean, |
| 272 | ): Event { |
| 273 | let event: Event; |
| 274 | |
| 275 | if (isErrorEvent(exception as ErrorEvent) && (exception as ErrorEvent).error) { |
| 276 | // If it is an ErrorEvent with `error` property, extract it to get actual Error |
| 277 | const errorEvent = exception as ErrorEvent; |
| 278 | return eventFromError(stackParser, errorEvent.error as Error); |
| 279 | } |
| 280 | |
| 281 | // If it is a `DOMError` (which is a legacy API, but still supported in some browsers) then we just extract the name |
| 282 | // and message, as it doesn't provide anything else. According to the spec, all `DOMExceptions` should also be |
| 283 | // `Error`s, but that's not the case in IE11, so in that case we treat it the same as we do a `DOMError`. |
| 284 | // |
| 285 | // https://developer.mozilla.org/en-US/docs/Web/API/DOMError |
| 286 | // https://developer.mozilla.org/en-US/docs/Web/API/DOMException |
| 287 | // https://webidl.spec.whatwg.org/#es-DOMException-specialness |
| 288 | if (isDOMError(exception) || isDOMException(exception as DOMException)) { |
| 289 | const domException = exception as DOMException; |
| 290 | |
| 291 | if ('stack' in (exception as Error)) { |
| 292 | event = eventFromError(stackParser, exception as Error); |
| 293 | |
| 294 | const firstException = event.exception?.values?.[0]; |
| 295 | if (attachStacktrace && syntheticException && firstException && !firstException.stacktrace) { |
| 296 | const frames = parseStackFrames(stackParser, syntheticException); |
| 297 | if (frames.length) { |
| 298 | firstException.stacktrace = { frames }; |
| 299 | addExceptionMechanism(event, { synthetic: true }); |
| 300 | } |
| 301 | } |
| 302 | } else { |
| 303 | const name = domException.name || (isDOMError(domException) ? 'DOMError' : 'DOMException'); |
| 304 | const message = domException.message ? `${name}: ${domException.message}` : name; |
| 305 | event = eventFromString(stackParser, message, syntheticException, attachStacktrace); |
| 306 | addExceptionTypeValue(event, message); |
| 307 | } |
| 308 | if ('code' in domException) { |
| 309 | // eslint-disable-next-line typescript/no-deprecated |
| 310 | event.tags = { ...event.tags, 'DOMException.code': `${domException.code}` }; |
| 311 | } |
| 312 | |
| 313 | return event; |
| 314 | } |
| 315 | if (isError(exception)) { |
| 316 | // we have a real Error object, do nothing |
| 317 | return eventFromError(stackParser, exception); |
| 318 | } |
| 319 | if (isPlainObject(exception) || isEvent(exception)) { |
| 320 | // If it's a plain object or an instance of `Event` (the built-in JS kind, not this SDK's `Event` type), serialize |
| 321 | // it manually. This will allow us to group events based on top-level keys which is much better than creating a new |
| 322 | // group on any key/value change. |
| 323 | const objectException = exception; |
no test coverage detected