import { Event , Exception , StackFrame , utils } from './shared' ; import { computeStackTrace , StackFrame as TraceKitStackFrame , StackTrace as TraceKitStackTrace } from './tracekit' ; const { extractExceptionKeysForMessage , isEvent , is } = utils ; const STACKTRACE_LIMIT = 50 ; param stacktrace hidden export function exceptionFromStacktrace ( stacktrace ) { const frames = prepareFramesForEvent ( stacktrace . stack ) ; const type = stacktrace . name ; const value = stacktrace . message ; const exception = { content : `${ type }:${ value }` , } ; if ( frames && frames . length ) { exception . stacktrace = frames ; } if ( type === undefined && value === '' ) { exception . content = 'Unrecoverable error caught' ; } return exception ; } hidden export function eventFromPlainObject ( exception , syntheticException ? , rejection ? ) { let type = isEvent ( exception ) ? exception . constructor . name : rejection ? 'UnhandledRejection' : 'Error' ; let value = `Non-Error ${ rejection ? 'promise rejection' : 'exception' } captured with keys is ${ extractExceptionKeysForMessage ( exception ) }` ; const event = { exceptions : [ { content : `${ type }:${ value }` , } , ] , } ; if ( syntheticException ) { const stacktrace = computeStackTrace ( syntheticException ) ; const frames = prepareFramesForEvent ( stacktrace . stack ) ; event . stacktrace = { frames , } ; } return event ; } hidden export function eventFromStacktrace ( stacktrace ) { const exceptions = exceptionFromStacktrace ( stacktrace ) ; return { exceptions : [ exceptions ] , } ; } 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 ( 'beidouWrapped' ) !== - 1 ) { localStack = localStack . slice ( 0 , - 1 ) ; } return localStack . slice ( 0 , STACKTRACE_LIMIT ) . map ( ( frame ) : StackFrame => ( { colno : frame . column === null ? undefined : frame . column , filename : frame . url || localStack [ 0 ] . url , function : frame . func || '?' , lineno : frame . line === null ? undefined : frame . line , } ) ) . reverse ( ) ; } hidden interface Data { [ key ] : any ; } export function responseFormat ( data = { } ) { if ( ! Object . keys ( data ) . length ) { return { } ; } let res = { } ; for ( let key in data ) { const type = is ( data [ key ] ) ; if ( type === 'Object' ) { res [ key ] = '[Object]' ; } else if ( type === 'Array' ) { res [ key ] = '[Array]' ; } else { res [ key ] = data [ key ] ; } } return res ; }