MCPcopy Index your code
hub / github.com/getsentry/sentry-javascript / eventFromUnknownInput

Function eventFromUnknownInput

packages/browser/src/eventbuilder.ts:266–347  ·  view source on GitHub ↗
(
  stackParser: StackParser,
  exception: unknown,
  syntheticException?: Error,
  attachStacktrace?: boolean,
  isUnhandledRejection?: boolean,
)

Source from the content-addressed store, hash-verified

264 * @hidden
265 */
266export 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;

Calls 12

eventFromErrorFunction · 0.85
isDOMErrorFunction · 0.85
isDOMExceptionFunction · 0.85
addExceptionMechanismFunction · 0.85
eventFromStringFunction · 0.85
addExceptionTypeValueFunction · 0.85
isErrorFunction · 0.85
isEventFunction · 0.85
eventFromPlainObjectFunction · 0.85
parseStackFramesFunction · 0.70
isErrorEventFunction · 0.50
isPlainObjectFunction · 0.50

Tested by

no test coverage detected