import { Event , Exception , StackFrame } from '@sentry/types' ; import { extractExceptionKeysForMessage , isEvent , normalizeToSize } from '@sentry/utils' ; import { computeStackTrace , StackFrame as TraceKitStackFrame , StackTrace as TraceKitStackTrace } from './tracekit' ; const STACKTRACE_LIMIT = 100 ; param stacktrace hidden export function exceptionFromStacktrace ( stacktrace ) { const frames = prepareFramesForEvent ( stacktrace . stack ) ; const exception = { type : stacktrace . name , value : stacktrace . message , } ; if ( frames && frames . length ) { exception . stacktrace = { frames } ; } if ( exception . type === undefined && exception . value === '' ) { exception . value = 'Unrecoverable error caught' ; } return exception ; } hidden export function eventFromPlainObject ( exception , syntheticException ? , rejection ? ) { const event = { exception : { values : [ { type : isEvent ( exception ) ? exception . constructor . name : rejection ? 'UnhandledRejection' : 'Error' , value : `Non-Error ${ rejection ? 'promise rejection' : 'exception' } captured with keys: ${ extractExceptionKeysForMessage ( exception ) }` , } , ] , } , extra : { __serialized__ : normalizeToSize ( exception ) , } , } ; if ( syntheticException ) { const stacktrace = computeStackTrace ( syntheticException ) ; const frames = prepareFramesForEvent ( stacktrace . stack ) ; event . stacktrace = { frames , } ; } return event ; } hidden export function eventFromStacktrace ( stacktrace ) { const exception = exceptionFromStacktrace ( stacktrace ) ; return { exception : { values : [ exception ] , } , } ; } hidden export function prepareFramesForEvent ( stack ) { if ( ! stack || ! stack . length ) { return [ ] ; } let localStack = stack ; const firstFrameFunction = localStack [ 0 ] . func || '' ; const lastFrameFunction = localStack [ localStack . length - 1 ] . func || '' ; if ( firstFrameFunction . indexOf ( 'captureMessage' ) !== - 1 || firstFrameFunction . indexOf ( 'captureException' ) !== - 1 ) { localStack = localStack . slice ( 1 ) ; } if ( lastFrameFunction . indexOf ( 'sentryWrapped' ) !== - 1 ) { localStack = localStack . slice ( 0 , - 1 ) ; } return localStack . map ( ( frame ) : StackFrame => ( { colno : frame . column === null ? undefined : frame . column , filename : frame . url || localStack [ 0 ] . url , function : frame . func || '?' , in_app : true , lineno : frame . line === null ? undefined : frame . line , } ) ) . slice ( 0 , STACKTRACE_LIMIT ) . reverse ( ) ; }