import { Event } from '@sentry/types' ; import { addExceptionMechanism , addExceptionTypeValue , isDOMError , isDOMException , isError , isErrorEvent , isEvent , isPlainObject , } from '@sentry/utils' ; import { eventFromPlainObject , eventFromStacktrace , prepareFramesForEvent } from './parsers' ; import { computeStackTrace } from './tracekit' ; /** JSDoc */ export function eventFromUnknownInput ( exception , syntheticException ? , options = { } ) { let event ; if ( isErrorEvent ( exception as ErrorEvent ) && ( exception as ErrorEvent ) . error ) { const errorEvent = exception as ErrorEvent ; exception = errorEvent . error ; event = eventFromStacktrace ( computeStackTrace ( exception as Error ) ) ; return event ; } if ( isDOMError ( exception as DOMError ) || isDOMException ( exception as DOMException ) ) { const domException = exception as DOMException ; const name = domException . name || ( isDOMError ( domException ) ? 'DOMError' : 'DOMException' ) ; const message = domException . message ? `${ name }: ${ domException . message }` : name ; event = eventFromString ( message , syntheticException , options ) ; addExceptionTypeValue ( event , message ) ; return event ; } if ( isError ( exception as Error ) ) { event = eventFromStacktrace ( computeStackTrace ( exception as Error ) ) ; return event ; } if ( isPlainObject ( exception ) || isEvent ( exception ) ) { const objectException = exception as { } ; event = eventFromPlainObject ( objectException , syntheticException , options . rejection ) ; addExceptionMechanism ( event , { synthetic : true , } ) ; return event ; } event = eventFromString ( exception as string , syntheticException , options ) ; addExceptionTypeValue ( event , `${ exception }` , undefined ) ; addExceptionMechanism ( event , { synthetic : true , } ) ; return event ; } /** JSDoc */ export function eventFromString ( input , syntheticException ? , options = { } ) { const event = { message : input , } ; if ( options . attachStacktrace && syntheticException ) { const stacktrace = computeStackTrace ( syntheticException ) ; const frames = prepareFramesForEvent ( stacktrace . stack ) ; event . stacktrace = { frames , } ; } return event ; }