format import { CancelledPromiseError , isError } from './errors' ; import { getLogger } from './Logger' ; type Result = { kind : 'success' ; } | { kind : 'cancelled' ; } | { kind : 'failure' ; supportedOperation : boolean ; error : any ; } ; export class UnsupportedError extends Error { constructor ( message ) { super ( message ) ; } } export function reportPlatformFailures < T > ( promise , name ) { return new Promise < T > ( ( resolve , reject ) => { promise . then ( ( fulfilledValue ) => { logPlatformSuccessRate ( name , { kind : 'success' } ) ; resolve ( fulfilledValue ) ; } ) . catch ( ( rejectionReason ) => { if ( rejectionReason instanceof CancelledPromiseError ) { logPlatformSuccessRate ( name , { kind : 'cancelled' , } ) ; } else { logPlatformSuccessRate ( name , { kind : 'failure' , supportedOperation : ! ( rejectionReason instanceof UnsupportedError ) , error : rejectionReason , } ) ; } reject ( rejectionReason ) ; } ) ; } ) ; } export function reportPluginFailures < T > ( promise , name , plugin ) { return promise . then ( ( fulfilledValue ) => { logPluginSuccessRate ( name , plugin , { kind : 'success' } ) ; return fulfilledValue ; } , ( rejectionReason ) => { if ( rejectionReason instanceof CancelledPromiseError ) { logPluginSuccessRate ( name , plugin , { kind : 'cancelled' , } ) ; } else { logPluginSuccessRate ( name , plugin , { kind : 'failure' , supportedOperation : ! ( rejectionReason instanceof UnsupportedError ) , error : rejectionReason , } ) ; } return Promise . reject ( rejectionReason ) ; } ) ; } export function tryCatchReportPlatformFailures < T > ( closure , name ) { try { const result = closure ( ) ; logPlatformSuccessRate ( name , { kind : 'success' } ) ; return result ; } catch ( e ) { logPlatformSuccessRate ( name , { kind : 'failure' , supportedOperation : ! ( e instanceof UnsupportedError ) , error : e , } ) ; throw e ; } } export function tryCatchReportPluginFailures < T > ( closure , name , plugin ) { try { const result = closure ( ) ; logPluginSuccessRate ( name , plugin , { kind : 'success' } ) ; return result ; } catch ( e ) { logPluginSuccessRate ( name , plugin , { kind : 'failure' , supportedOperation : ! ( e instanceof UnsupportedError ) , error : e , } ) ; throw e ; } } export async function tryCatchReportPluginFailuresAsync < T > ( closure , name , plugin ) { try { const result = await closure ( ) ; logPluginSuccessRate ( name , plugin , { kind : 'success' } ) ; return result ; } catch ( e ) { logPluginSuccessRate ( name , plugin , { kind : 'failure' , supportedOperation : ! ( e instanceof UnsupportedError ) , error : e , } ) ; throw e ; } } param action param data export function reportUsage ( action , data ? , plugin ? ) { getLogger ( ) . track ( 'usage' , action , data , plugin ) ; } export function logPlatformSuccessRate ( name , result ) { if ( result . kind === 'success' ) { getLogger ( ) . track ( 'success-rate' , name , { value : 1 } ) ; } else if ( result . kind === 'cancelled' ) { getLogger ( ) . track ( 'operation-cancelled' , name ) ; } else { getLogger ( ) . track ( 'success-rate' , name , { value : 0 , supportedOperation : result . supportedOperation ? 1 : 0 , error : extractMessage ( result . error ) , } ) ; } } function logPluginSuccessRate ( name , plugin , result ) { if ( result . kind === 'success' ) { getLogger ( ) . track ( 'success-rate' , name , { value : 1 } , plugin ) ; } else if ( result . kind === 'cancelled' ) { getLogger ( ) . track ( 'operation-cancelled' , name , undefined , plugin ) ; } else { getLogger ( ) . track ( 'success-rate' , name , { value : 0 , supportedOperation : result . supportedOperation ? 1 : 0 , error : extractMessage ( result . error ) , } , plugin ) ; } } function extractMessage ( error ) { if ( isError ( error ) ) { return error . message ; } return JSON . stringify ( error ) ; }