| import config from '@automattic/calypso-config'; |
| import { captureException } from '@automattic/calypso-sentry'; |
| import { |
| translateCheckoutPaymentMethodToWpcomPaymentMethod, |
| isRedirectPaymentMethod, |
| } from '@automattic/wpcom-checkout'; |
| import { logToLogstash } from 'calypso/lib/logstash'; |
| import { recordTracksEvent } from 'calypso/state/analytics/actions'; |
| import type { CheckoutPaymentMethodSlug } from '@automattic/wpcom-checkout'; |
| import type { CalypsoDispatch } from 'calypso/state/types'; |
|
|
| function serializeCaughtError( |
| |
| |
| |
| |
| error: unknown, |
| options: { |
| excludeStack?: boolean; |
| } = {} |
| ): string { |
| const messages = []; |
| messages.push( getErrorMessageFromError( error ) ); |
| let hasCause = false; |
| const errorObject = error as Error; |
| if ( 'cause' in errorObject && errorObject.cause ) { |
| hasCause = true; |
| const cause = serializeCaughtError( errorObject.cause, options ); |
| messages.push( `(Cause: ${ cause })` ); |
| } |
| |
| |
| |
| if ( ! options?.excludeStack && ! hasCause && 'stack' in errorObject && errorObject.stack ) { |
| messages.push( `(Stack: ${ errorObject.stack })` ); |
| } |
| return messages.join( '; ' ); |
| } |
|
|
| function getErrorMessageFromError( error: unknown ): string { |
| const errorObject = error as Error; |
| if ( 'message' in errorObject && errorObject.message ) { |
| return `Message: ${ errorObject.message }`; |
| } |
| return `Non-Error: ${ error }`; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export function convertErrorToString( error: Error ): string { |
| return serializeCaughtError( error ); |
| } |
|
|
| export function logStashLoadErrorEvent( |
| errorType: string, |
| error: Error, |
| additionalData: Record< string, string | number | undefined > = {} |
| ): Promise< void > { |
| captureException( error, { |
| tags: { |
| serialized_message: serializeCaughtError( error, { excludeStack: true } ), |
| calypso_checkout: 'true', |
| error_type: errorType, |
| ...additionalData, |
| }, |
| } ); |
| return logStashEvent( 'composite checkout load error', { |
| ...additionalData, |
| type: errorType, |
| message: additionalData.message |
| ? String( additionalData.message ) |
| : convertErrorToString( error ), |
| ...( additionalData.message |
| ? |
| |
| { |
| errorMessage: convertErrorToString( error ), |
| } |
| : {} ), |
| tags: [ 'checkout-error-boundary' ], |
| } ); |
| } |
|
|
| export type DataForLog = Record< string, string | string[] > & { tags?: string[] }; |
|
|
| export function logStashEvent( |
| message: string, |
| dataForLog: DataForLog, |
| severity: 'error' | 'warning' | 'info' = 'error' |
| ): Promise< void > { |
| const tags = dataForLog.tags ?? []; |
| const extra: Record< string, string | string[] > = { |
| env: config( 'env_id' ), |
| }; |
| Object.keys( dataForLog ).forEach( ( key ) => { |
| if ( key === 'tags' ) { |
| return; |
| } |
| extra[ key ] = dataForLog[ key ]; |
| } ); |
| return logToLogstash( { |
| feature: 'calypso_client', |
| message, |
| severity: config( 'env_id' ) === 'production' ? severity : 'debug', |
| extra, |
| tags, |
| } ); |
| } |
|
|
| export const recordCompositeCheckoutErrorDuringAnalytics = |
| ( { errorObject, failureDescription }: { errorObject: Error; failureDescription: string } ) => |
| ( dispatch: CalypsoDispatch ): void => { |
| |
| |
| |
| |
| dispatch( |
| recordTracksEvent( 'calypso_checkout_composite_error', { |
| error_message: ( errorObject as Error ).message, |
| action_type: failureDescription, |
| } ) |
| ); |
| logStashLoadErrorEvent( 'calypso_checkout_composite_error', errorObject, { |
| action_type: failureDescription, |
| } ); |
| }; |
|
|
| export const recordTransactionBeginAnalytics = |
| ( { |
| paymentMethodId, |
| useForAllSubscriptions, |
| }: { |
| paymentMethodId: CheckoutPaymentMethodSlug; |
| useForAllSubscriptions?: boolean; |
| } ) => |
| ( dispatch: CalypsoDispatch ): void => { |
| try { |
| if ( isRedirectPaymentMethod( paymentMethodId ) ) { |
| dispatch( recordTracksEvent( 'calypso_checkout_form_redirect', {} ) ); |
| } |
| dispatch( |
| recordTracksEvent( 'calypso_checkout_form_submit', { |
| credits: null, |
| payment_method: |
| translateCheckoutPaymentMethodToWpcomPaymentMethod( paymentMethodId ) || '', |
| ...( useForAllSubscriptions ? { use_for_all_subs: useForAllSubscriptions } : undefined ), |
| } ) |
| ); |
| dispatch( |
| recordTracksEvent( 'calypso_checkout_composite_form_submit', { |
| credits: null, |
| payment_method: |
| translateCheckoutPaymentMethodToWpcomPaymentMethod( paymentMethodId ) || '', |
| ...( useForAllSubscriptions ? { use_for_all_subs: useForAllSubscriptions } : undefined ), |
| } ) |
| ); |
| const paymentMethodIdForTracks = paymentMethodId.startsWith( 'existingCard' ) |
| ? 'existing_card' |
| : paymentMethodId.replace( /-/, '_' ).toLowerCase(); |
| dispatch( |
| recordTracksEvent( |
| `calypso_checkout_composite_${ paymentMethodIdForTracks }_submit_clicked`, |
| {} |
| ) |
| ); |
| } catch ( errorObject ) { |
| dispatch( |
| recordCompositeCheckoutErrorDuringAnalytics( { |
| errorObject: errorObject as Error, |
| failureDescription: `transaction-begin: ${ paymentMethodId }`, |
| } ) |
| ); |
| } |
| }; |
|
|