Skip to content

Commit

Permalink
fix(background/paymentSession): handle Invalid Token error (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi authored Jul 16, 2024
1 parent ed9dc97 commit 2eb9e5f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
19 changes: 14 additions & 5 deletions src/background/services/openPayments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,9 @@ export class OpenPaymentsService {
walletAddress,
amount: transformedAmount
}).catch((err) => {
if (err instanceof OpenPaymentsClientError) {
if (err.status === 400 && err.code === 'invalid_client') {
const msg = this.t('connectWallet_error_invalidClient')
throw new Error(msg, { cause: err })
}
if (isInvalidClientError(err)) {
const msg = this.t('connectWallet_error_invalidClient')
throw new Error(msg, { cause: err })
}
throw err
})
Expand Down Expand Up @@ -630,3 +628,14 @@ export const isSignatureValidationError = (error: any) => {
error.description?.includes('Signature validation error')
)
}

export const isTokenExpiredError = (error: any) => {
if (!isOpenPaymentsClientError(error)) return false
return isTokenInvalidError(error) || isTokenInactiveError(error)
}
export const isTokenInvalidError = (error: OpenPaymentsClientError) => {
return error.status === 401 && error.description === 'Invalid Token'
}
export const isTokenInactiveError = (error: OpenPaymentsClientError) => {
return error.status === 403 && error.description === 'Inactive Token'
}
18 changes: 6 additions & 12 deletions src/background/services/paymentSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { OpenPaymentsClientError } from '@interledger/open-payments/dist/client'
import { sendMonetizationEvent } from '../lib/messages'
import { convert, sleep } from '@/shared/helpers'
import { transformBalance } from '@/popup/lib/utils'
import { isKeyRevokedError } from './openPayments'
import { isKeyRevokedError, isTokenExpiredError } from './openPayments'
import type { EventsService, OpenPaymentsService, TabState } from '.'
import type { Tabs } from 'webextension-polyfill'
import type { MonetizationEventDetails } from '@/shared/messages'
Expand Down Expand Up @@ -158,13 +158,10 @@ export class PaymentSession {
} catch (e) {
if (isKeyRevokedError(e)) {
this.events.emit('open_payments.key_revoked')
} else if (isTokenExpiredError(e)) {
await this.openPaymentsService.rotateToken()
continue
} else if (e instanceof OpenPaymentsClientError) {
// Status code 403 -> expired access token
if (e.status === 403) {
await this.openPaymentsService.rotateToken()
continue
}

// We need better error handling.
if (e.status === 400) {
await this.setIncomingPaymentUrl()
Expand Down Expand Up @@ -302,11 +299,8 @@ export class PaymentSession {
} catch (e) {
if (isKeyRevokedError(e)) {
this.events.emit('open_payments.key_revoked')
} else if (e instanceof OpenPaymentsClientError) {
// Status code 403 -> expired access token
if (e.status === 403) {
await this.openPaymentsService.rotateToken()
}
} else if (isTokenExpiredError(e)) {
await this.openPaymentsService.rotateToken()
}
} finally {
if (outgoingPayment) {
Expand Down

0 comments on commit 2eb9e5f

Please sign in to comment.