Skip to content

Commit 53f4a69

Browse files
authored
refactor: simplify and add failure/cancel error utils (#1324)
1 parent d995394 commit 53f4a69

8 files changed

Lines changed: 230 additions & 204 deletions

File tree

src/background/services/keyAutoAdd.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88
type ErrorWithKeyLike,
99
type I18nInfo,
1010
} from '@/shared/helpers';
11-
import { createTab } from '@/background/utils';
11+
import {
12+
createTab,
13+
WalletStatusCancelError,
14+
WalletStatusFailureError,
15+
} from '@/background/utils';
1216
import type { Browser, Runtime, Scripting } from 'webextension-polyfill';
1317
import type { WalletStatus, TabId, WalletInfo } from '@/shared/types';
1418
import type { Cradle } from '@/background/container';
@@ -90,7 +94,7 @@ export class KeyAutoAddService {
9094
const BASE_TIMEOUT = 5 * 1000;
9195
const timeout = new Timeout(BASE_TIMEOUT, () => {
9296
removeListeners();
93-
reject(new ErrorWithKey('connectWallet_error_timeout'));
97+
reject(new WalletStatusFailureError('timeout'));
9498
});
9599

96100
const tabID = await createTab(this.browser, url);
@@ -105,7 +109,7 @@ export class KeyAutoAddService {
105109
const onTabCloseListener: OnTabRemovedCallback = (tabId) => {
106110
if (tabId !== tabID) return;
107111
removeListeners();
108-
reject(new ErrorWithKey('connectWallet_error_tabClosed'));
112+
reject(new WalletStatusCancelError('tab_closed'));
109113
};
110114

111115
const ports = new Set<Runtime.Port>();

src/background/services/outgoingPaymentGrant.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ import {
2121
isInvalidContinuationError,
2222
isNotFoundError,
2323
} from '@/background/services/openPayments';
24-
import { createTabIfNotExists } from '@/background/utils';
24+
import {
25+
createTabIfNotExists,
26+
WalletStatusCancelError,
27+
WalletStatusFailureError,
28+
} from '@/background/utils';
2529

2630
interface InteractionParams {
2731
interactRef: string;
@@ -284,7 +288,7 @@ export class OutgoingPaymentGrantService {
284288
if (tabId !== tabID) return;
285289

286290
removeListeners();
287-
reject(new ErrorWithKey('connectWallet_error_tabClosed'));
291+
reject(new WalletStatusCancelError('tab_closed'));
288292
};
289293

290294
const getInteractionInfo: TabUpdateCallback = async (tabId, changeInfo) => {
@@ -306,9 +310,9 @@ export class OutgoingPaymentGrantService {
306310
if (interactRef && hash) {
307311
resolve({ interactRef, hash, tabId });
308312
} else if (result === 'grant_rejected') {
309-
reject(new ErrorWithKey('connectWallet_error_grantRejected'));
313+
reject(new WalletStatusCancelError('grant_rejected'));
310314
} else if (result === 'grant_invalid') {
311-
reject(new ErrorWithKey('connectWallet_error_grantInvalid'));
315+
reject(new WalletStatusFailureError('grant_invalid'));
312316
}
313317
} catch {
314318
/* do nothing */
@@ -337,7 +341,7 @@ export class OutgoingPaymentGrantService {
337341
'verifyInteractionHash failed with authServer without trailing slash',
338342
);
339343
if (hash === (await computeHash(ensureEnd(authServer, '/')))) return;
340-
throw new ErrorWithKey('connectWallet_error_hashFailed');
344+
throw new WalletStatusFailureError('grant_hash_failed');
341345
}
342346

343347
private computeHash = async (
@@ -365,10 +369,11 @@ export class OutgoingPaymentGrantService {
365369

366370
return continuation;
367371
} catch (error) {
368-
this.logger.error('connectWallet_error_continuationFailed', {
369-
cause: error,
372+
const err = new WalletStatusFailureError('grant_continuation_failed', {
373+
details: error,
370374
});
371-
throw new ErrorWithKey('connectWallet_error_continuationFailed');
375+
this.logger.error(err);
376+
throw err;
372377
}
373378
}
374379

0 commit comments

Comments
 (0)