From 2eb9e5f06b70001310b8204669a1d432d12922e1 Mon Sep 17 00:00:00 2001 From: Sid Vishnoi <8426945+sidvishnoi@users.noreply.github.com> Date: Tue, 16 Jul 2024 13:02:51 +0530 Subject: [PATCH] fix(background/paymentSession): handle Invalid Token error (#428) --- src/background/services/openPayments.ts | 19 ++++++++++++++----- src/background/services/paymentSession.ts | 18 ++++++------------ 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/background/services/openPayments.ts b/src/background/services/openPayments.ts index e4bf138a..232a683f 100644 --- a/src/background/services/openPayments.ts +++ b/src/background/services/openPayments.ts @@ -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 }) @@ -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' +} diff --git a/src/background/services/paymentSession.ts b/src/background/services/paymentSession.ts index dbc82cbc..380199ec 100644 --- a/src/background/services/paymentSession.ts +++ b/src/background/services/paymentSession.ts @@ -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' @@ -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() @@ -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) {