Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PAY-2041] Connect stripe session creation errors to analytics #6465

Merged
merged 3 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/common/src/models/Analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ export enum Name {
BUY_USDC_RECOVERY_SUCCESS = 'Buy USDC: Recovery Success',
BUY_USDC_RECOVERY_FAILURE = 'Buy USDC: Recovery Failure',

// Stripe Tracking
STRIPE_SESSION_CREATION_ERROR = 'Stripe: Session Creation Error',

// Purchase Content
PURCHASE_CONTENT_STARTED = 'Purchase Content: Started',
PURCHASE_CONTENT_SUCCESS = 'Purchase Content: Success',
Expand Down Expand Up @@ -1574,6 +1577,7 @@ type BuyAudioRecoveryFailure = {
error: string
}

// Buy USDC
type BuyUSDCOnRampOpened = {
eventName: Name.BUY_USDC_ON_RAMP_OPENED
provider: string
Expand Down Expand Up @@ -1623,6 +1627,18 @@ type BuyUSDCRecoveryFailure = {
error: string
}

// Stripe
type StripeSessionCreationError = {
eventName: Name.STRIPE_SESSION_CREATION_ERROR
amount: string
destinationCurrency: string
code: string
errorMessage: string
type: string
}

// Content Purchase

type ContentPurchaseMetadata = {
price: number
contentId: number
Expand Down Expand Up @@ -1983,6 +1999,7 @@ export type AllTrackingEvents =
| BuyUSDCRecoveryInProgress
| BuyUSDCRecoverySuccess
| BuyUSDCRecoveryFailure
| StripeSessionCreationError
| PurchaseContentStarted
| PurchaseContentSuccess
| PurchaseContentFailure
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/store/buy-usdc/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { call, select } from 'typed-redux-saga'

import { createUserBankIfNeeded } from 'services/audius-backend/solana'
import { IntKeys } from 'services/index'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??? how did this ever work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do remember a time where circular dependencies would cause serious issues. I think our build tooling at this point is advanced enough to work around it and just generate a warning. FWIW, we have a few other long-standing instances of this.
I have worked on teams in the past where we didn't use index files for exactly this reason, preferring direct module imports instead.
Though, in this case, since we're bundling everything up into a "package", the index files serve as a way to join the things we want to be finally exported at the top level.

I would say when we un-package common, we might do well to get rid of index files :-)

import { IntKeys } from 'services/remote-config'
import {
MAX_CONTENT_PRICE_CENTS,
MAX_USDC_PURCHASE_AMOUNT_CENTS,
Expand Down
48 changes: 44 additions & 4 deletions packages/common/src/store/ui/stripe-modal/sagas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { IdentityRequestError } from '@audius/sdk'
import { call, takeEvery, put, select } from 'typed-redux-saga'

import { Name } from 'models/Analytics'
import { ErrorLevel } from 'models/ErrorReporting'
import { createStripeSession } from 'services/audius-backend/stripe'
import { getContext } from 'store/effects'

Expand All @@ -12,11 +15,17 @@ import {
stripeSessionCreated,
stripeSessionStatusChanged
} from './slice'
import {
StripeSessionCreationError,
StripeSessionCreationErrorResponseData
} from './types'

function* handleInitializeStripeModal({
payload: { amount, destinationCurrency, destinationWallet }
}: ReturnType<typeof initializeStripeModal>) {
const audiusBackendInstance = yield* getContext('audiusBackendInstance')
const reportToSentry = yield* getContext('reportToSentry')
const { track, make } = yield* getContext('analytics')
const { onrampFailed } = yield* select(getStripeModalState)
try {
const res = yield* call(createStripeSession, audiusBackendInstance, {
Expand All @@ -26,13 +35,44 @@ function* handleInitializeStripeModal({
})
yield* put(stripeSessionCreated({ clientSecret: res.client_secret }))
} catch (e) {
// TODO: When we have better error messages from identity, we should extract them here so
// they make it into analytics.
// https://linear.app/audius/issue/PAY-2041/[usdc]-we-should-pipe-the-stripe-session-creation-error-back-from
const {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't you love it when you delete the TODOs

code,
message: errorMessage,
type
} = ((e as IdentityRequestError).response?.data ??
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not my favorite, but casting errors in catch blocks never feels perfect.

{}) as StripeSessionCreationErrorResponseData

const error = new StripeSessionCreationError(code, errorMessage, type)

if (onrampFailed) {
yield* put({ type: onrampFailed.type, payload: { error: e } })
yield* put({
type: onrampFailed.type,
payload: { error }
})
}
yield* put(setVisibility({ modal: 'StripeOnRamp', visible: 'closing' }))
yield* call(reportToSentry, {
level: ErrorLevel.Error,
error,
additionalInfo: {
code,
errorMessage,
type,
amount,
destinationCurrency
}
})
yield* call(
track,
make({
eventName: Name.STRIPE_SESSION_CREATION_ERROR,
amount,
code,
destinationCurrency,
errorMessage,
type
})
)
}
}

Expand Down
18 changes: 18 additions & 0 deletions packages/common/src/store/ui/stripe-modal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ export type StripeModalState = {
stripeSessionStatus?: StripeSessionStatus
stripeClientSecret?: string
}

export type StripeSessionCreationErrorResponseData = {
error: string
code: string
message: string
type: string
}

export class StripeSessionCreationError extends Error {
constructor(
public code: string,
// Avoiding `message` to not shadow the `message` property on Error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, good catch. maybe should be stripeErrorMessage?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that works too. I think I was going for a "generic" pattern we could use in the future. But that's not really necessary and your suggestion makes it clear where the error comes from.

public errorMessage: string,
public type: string
) {
super(`Failed to create Stripe session: ${errorMessage}`)
}
}
2 changes: 2 additions & 0 deletions packages/libs/src/AudiusLibs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { EthContracts } from './services/ethContracts'
import { EthWeb3Config, EthWeb3Manager } from './services/ethWeb3Manager'
import { Hedgehog, HedgehogConfig } from './services/hedgehog'
import { IdentityService } from './services/identity'
import type { IdentityRequestError } from './services/identity'
import { Schemas, SchemaValidator } from './services/schemaValidator'
import {
SolanaWeb3Manager,
Expand Down Expand Up @@ -637,6 +638,7 @@ export class AudiusLibs {
}

export { AudiusABIDecoder, Utils, SolanaUtils, CreatorNode }
export { IdentityRequestError }

export { SanityChecks } from './sanityChecks'
export { RewardsAttester, DEFAULT_MINT, MintName } from './services/solana'
2 changes: 2 additions & 0 deletions packages/libs/src/services/identity/IdentityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export type RelayTransactionData = {
lookupTableAddresses?: string[]
}

export type IdentityRequestError = AxiosError

type AttestationResult = {
status: string
userId: string
Expand Down