File size: 6,054 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
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(
// This may come from Error.cause which I'm pretty sure has no defined
// type. It can be used to keep another Error but it could also be anything
// else so let's not make any assumptions. Also things other than Error
// objects can be thrown so let's not even assume this is an Error.
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 })` );
}
// We only include the stack trace if there is no cause, meaning this is
// the deepest error in the chain. The others are all likely re-thrown
// errors so we should know their stack trace already.
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 }`;
}
/**
* Convert a thrown error to a string for logging.
*
* I've typed this function as requiring an Error because that's the intended
* behavior and I'd like TypeScript to enforce that as best as it can. However,
* other things can be thrown besides Error objects and so this will actually
* handle other things like strings just fine.
*/
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
? // No need to log the `errorMessage` separately if it's the same as
// the `message` property.
{
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 => {
// This is a fallback to catch any errors caused by the analytics code
// Anything in this block should remain very simple and extremely
// tolerant of any kind of data. It should make no assumptions about
// the data it uses. There's no fallback for the fallback!
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 }`,
} )
);
}
};
|