import { getCurrentHub } from '@sentry/browser' ; import { formatComponentName , generateComponentTrace } from './components' ; import { Options , ViewModel , Vue } from './types' ; type UnknownFunc = ( ... args : unknown [ ] ) => void ; export const attachErrorHandler = ( app , options ) : void => { const { errorHandler , warnHandler , silent } = app . config ; app . config . errorHandler = ( error , vm , lifecycleHook ) : void => { const componentName = formatComponentName ( vm , false ) ; const trace = vm ? generateComponentTrace ( vm ) : '' ; const metadata = { componentName , lifecycleHook , trace , } ; if ( vm && options . attachProps ) { metadata . propsData = vm . $options . propsData || vm . $props ; } setTimeout ( ( ) => { getCurrentHub ( ) . withScope ( scope => { scope . setContext ( 'vue' , metadata ) ; getCurrentHub ( ) . captureException ( error ) ; } ) ; } ) ; if ( typeof errorHandler === 'function' ) { ( errorHandler as UnknownFunc ) . call ( app , error , vm , lifecycleHook ) ; } if ( options . logErrors ) { const hasConsole = typeof console !== 'undefined' ; const message = `Error in ${ lifecycleHook }: "${ error && error . toString ( ) }"` ; if ( warnHandler ) { ( warnHandler as UnknownFunc ) . call ( null , message , vm , trace ) ; } else if ( hasConsole && ! silent ) { console . error ( `[Vue warn]: ${ message }${ trace }` ) ; } } } ; } ;