|  | 
|  | 1 | +import { EventHint, Mechanism, Options } from '@sentry/types'; | 
|  | 2 | +import { isError, isPlainObject, extractExceptionKeysForMessage, normalizeToSize, SyncPromise } from '@sentry/utils'; | 
|  | 3 | +import { getCurrentHub } from '@sentry/hub'; | 
|  | 4 | +import { parseError } from './parsers'; | 
|  | 5 | + | 
|  | 6 | +/** | 
|  | 7 | + * Builds and Event from a Exception | 
|  | 8 | + * @hidden | 
|  | 9 | + */ | 
|  | 10 | +function eventFromException(options: Options, exception: unknown, hint?: EventHint): PromiseLike<Event> { | 
|  | 11 | +  // eslint-disable-next-line @typescript-eslint/no-explicit-any | 
|  | 12 | +  let ex: any = exception; | 
|  | 13 | +  const providedMechanism: Mechanism | undefined = | 
|  | 14 | +    hint && hint.data && (hint.data as { mechanism: Mechanism }).mechanism; | 
|  | 15 | +  const mechanism: Mechanism = providedMechanism || { | 
|  | 16 | +    handled: true, | 
|  | 17 | +    type: 'generic', | 
|  | 18 | +  }; | 
|  | 19 | + | 
|  | 20 | +  if (!isError(exception)) { | 
|  | 21 | +    if (isPlainObject(exception)) { | 
|  | 22 | +      // This will allow us to group events based on top-level keys | 
|  | 23 | +      // which is much better than creating new group when any key/value change | 
|  | 24 | +      const message = `Non-Error exception captured with keys: ${extractExceptionKeysForMessage(exception)}`; | 
|  | 25 | + | 
|  | 26 | +      getCurrentHub().configureScope(scope => { | 
|  | 27 | +        scope.setExtra('__serialized__', normalizeToSize(exception as Record<string, unknown>)); | 
|  | 28 | +      }); | 
|  | 29 | + | 
|  | 30 | +      ex = (hint && hint.syntheticException) || new Error(message); | 
|  | 31 | +      (ex as Error).message = message; | 
|  | 32 | +    } else { | 
|  | 33 | +      // This handles when someone does: `throw "something awesome";` | 
|  | 34 | +      // We use synthesized Error here so we can extract a (rough) stack trace. | 
|  | 35 | +      ex = (hint && hint.syntheticException) || new Error(exception as string); | 
|  | 36 | +      (ex as Error).message = exception as string; | 
|  | 37 | +    } | 
|  | 38 | +    mechanism.synthetic = true; | 
|  | 39 | +  } | 
|  | 40 | + | 
|  | 41 | +  return new SyncPromise<Event>((resolve, reject) => | 
|  | 42 | +    parseError(ex as Error, options) | 
|  | 43 | +      .then(event => { | 
|  | 44 | +        addExceptionTypeValue(event, undefined, undefined); | 
|  | 45 | +        addExceptionMechanism(event, mechanism); | 
|  | 46 | + | 
|  | 47 | +        resolve({ | 
|  | 48 | +          ...event, | 
|  | 49 | +          event_id: hint && hint.event_id, | 
|  | 50 | +        }); | 
|  | 51 | +      }) | 
|  | 52 | +      .then(null, reject), | 
|  | 53 | +  ); | 
|  | 54 | +} | 
0 commit comments