@@ -23,7 +23,10 @@ import {
2323} from '@/background/utils' ;
2424import { KeyAutoAddService } from '@/background/services/keyAutoAdd' ;
2525import { generateEd25519KeyPair , exportJWK } from '@/shared/crypto' ;
26- import { isInvalidClientError } from '@/background/services/openPayments' ;
26+ import {
27+ isInvalidClientError ,
28+ isOpenPaymentsClientError ,
29+ } from '@/background/services/openPayments' ;
2730import { APP_URL } from '@/background/constants' ;
2831import { bytesToHex } from '@noble/hashes/utils' ;
2932import type { Cradle } from '@/background/container' ;
@@ -37,6 +40,7 @@ export class WalletService {
3740 private storage : Cradle [ 'storage' ] ;
3841 private events : Cradle [ 'events' ] ;
3942 private browser : Cradle [ 'browser' ] ;
43+ private logger : Cradle [ 'logger' ] ;
4044 private appName : Cradle [ 'appName' ] ;
4145 private browserName : Cradle [ 'browserName' ] ;
4246 private t : Cradle [ 't' ] ;
@@ -47,6 +51,7 @@ export class WalletService {
4751 storage,
4852 events,
4953 browser,
54+ logger,
5055 appName,
5156 browserName,
5257 t,
@@ -57,6 +62,7 @@ export class WalletService {
5762 storage,
5863 events,
5964 browser,
65+ logger,
6066 appName,
6167 browserName,
6268 t,
@@ -245,22 +251,41 @@ export class WalletService {
245251 this . resetConnectState ( ) ;
246252 }
247253
248- async disconnectWallet ( ) {
254+ async disconnectWallet ( force = false ) {
249255 const { recurringGrant, oneTimeGrant } = await this . storage . get ( [
250256 'recurringGrant' ,
251257 'oneTimeGrant' ,
252258 ] ) ;
253259 if ( ! recurringGrant && ! oneTimeGrant ) {
254260 return ;
255261 }
262+
263+ const handleError = (
264+ err : unknown ,
265+ grantType : 'recurring' | 'oneTime' ,
266+ force : boolean ,
267+ ) => {
268+ this . logger . error ( `Could not cancel ${ grantType } grant` , { err } ) ;
269+ if ( force ) return ;
270+
271+ if ( isOpenPaymentsClientError ( err ) ) {
272+ throw new ErrorWithKey ( 'disconnectWallet_error_generic' , [
273+ err . status ? `HTTP ${ err . status } - ${ err . message } ` : err . message ,
274+ ] ) ;
275+ }
276+ throw err ;
277+ } ;
278+
256279 if ( recurringGrant ) {
257- await this . outgoingPaymentGrantService . cancelGrant (
258- recurringGrant . continue ,
259- ) ;
280+ await this . outgoingPaymentGrantService
281+ . cancelGrant ( recurringGrant . continue )
282+ . catch ( ( err ) => handleError ( err , 'recurring' , force ) ) ;
260283 this . outgoingPaymentGrantService . disableRecurringGrant ( ) ;
261284 }
262285 if ( oneTimeGrant ) {
263- await this . outgoingPaymentGrantService . cancelGrant ( oneTimeGrant . continue ) ;
286+ await this . outgoingPaymentGrantService
287+ . cancelGrant ( oneTimeGrant . continue )
288+ . catch ( ( err ) => handleError ( err , 'recurring' , force ) ) ;
264289 this . outgoingPaymentGrantService . disableOneTimeGrant ( ) ;
265290 }
266291 await this . storage . clear ( ) ;
0 commit comments