diff --git a/src/locales/en/main.json b/src/locales/en/main.json index f0512f75077..1cfccbf34e1 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -3020,6 +3020,7 @@ "topupSuccess": "Credits added successfully", "topupFailed": "Top-up failed", "topupTimeout": "Top-up verification timed out", + "topupAlreadyPendingDetail": "A top-up you started earlier is still going through. Finish verifying it or wait for it to complete before adding more credits.", "paymentDeclinedDetail": "Your bank declined this payment. Try another payment method or contact your bank.", "insufficientFundsDetail": "This payment method has insufficient funds. Try another payment method or contact your bank.", "expiredCardDetail": "This card has expired. Use a different payment method.", diff --git a/src/platform/workspace/billing/sdk/topupOperationView.test.ts b/src/platform/workspace/billing/sdk/topupOperationView.test.ts index 40bc237e5f6..1f619ed0f23 100644 --- a/src/platform/workspace/billing/sdk/topupOperationView.test.ts +++ b/src/platform/workspace/billing/sdk/topupOperationView.test.ts @@ -116,6 +116,23 @@ describe('projectTopupResult', () => { ) }) + it('names the earlier top-up still settling instead of an unknown error', () => { + expect(() => + projectTopupResult( + { status: 'error', code: 'OPERATION_ALREADY_PENDING' }, + 1000 + ) + ).toThrow( + expect.objectContaining({ + name: 'WorkspaceApiError', + code: 'OPERATION_ALREADY_PENDING', + failureCategory: 'stale_operation', + message: + 'A top-up you started earlier is still going through. Finish verifying it or wait for it to complete before adding more credits.' + }) + ) + }) + it('carries the HTTP status of a transport failure', () => { let thrown: unknown try { diff --git a/src/platform/workspace/billing/sdk/topupOperationView.ts b/src/platform/workspace/billing/sdk/topupOperationView.ts index 3a96a41db05..b47f9be8e69 100644 --- a/src/platform/workspace/billing/sdk/topupOperationView.ts +++ b/src/platform/workspace/billing/sdk/topupOperationView.ts @@ -90,11 +90,15 @@ export class UncreditedTopupResponse implements CreateTopupResponse { ) {} } -/** - * Most SDK refusals carry no HTTP status, which a status-less API error - * would otherwise report as a network failure, so each names its category. - */ function topupFailureError(failure: TopupFailure): WorkspaceApiError { + if (failure.code === 'OPERATION_ALREADY_PENDING') { + return new WorkspaceApiError( + t('billingOperation.topupAlreadyPendingDetail'), + undefined, + failure.code, + 'stale_operation' + ) + } const serverCode = 'serverCode' in failure ? failure.serverCode : undefined return new WorkspaceApiError( t('credits.topUp.unknownError'),