import { eventFromPlainObject , eventFromStacktrace , prepareFramesForEvent } from './parsers' ; import { Event , EventHint , Exception , Options , utils } from './shared' ; import { computeStackTrace } from './tracekit' ; const { addExceptionMechanism , addExceptionTypeValue , isDOMError , isDOMException , isError , isErrorEvent , isEvent , isPlainObject , SyncPromise , } = utils ; hidden export function eventFromException ( options , exception , hint ? ) { const syntheticException = ( hint && hint . syntheticException ) || undefined ; const event = eventFromUnknownInput ( exception , syntheticException , { attachStacktrace : options . attachStacktrace , } ) ; return SyncPromise . resolve ( event ) ; } hidden export function eventFromMessage ( options , message , hint ? ) { const syntheticException = ( hint && hint . syntheticException ) || undefined ; const event = eventFromString ( message , syntheticException , { attachStacktrace : options . attachStacktrace , } ) ; return SyncPromise . resolve ( event ) ; } hidden 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 Record < string , unknown > ; event = eventFromPlainObject ( objectException , syntheticException , options . rejection ) ; return event ; } event = eventFromString ( exception as string , syntheticException , options ) ; addExceptionTypeValue ( event , `${ exception }` , undefined ) ; addExceptionMechanism ( event , { synthetic : true , } ) ; return event ; } hidden export function eventFromString ( input , syntheticException ? , options = { } ) { const ERROR_TYPES_RE = /^(?:[Uu]ncaught (?:exception: )?)?(?:((?:Eval|Internal|Range|Reference|Syntax|Type|URI|)Error): )?(.*)$/i ; let name ; let message = input ; const groups = input . match ( ERROR_TYPES_RE ) ; if ( groups ) { name = groups [ 1 ] ; message = groups [ 2 ] ; } const exceptions = { content : `${ name || 'Error' }:${ message }` , } ; if ( options . attachStacktrace && syntheticException ) { const stacktrace = computeStackTrace ( syntheticException ) ; exceptions . stacktrace = prepareFramesForEvent ( stacktrace . stack ) ; } return { exceptions : [ exceptions ] } ; }