-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy patherrors.test.ts
More file actions
81 lines (66 loc) · 2.7 KB
/
Copy patherrors.test.ts
File metadata and controls
81 lines (66 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import {
ErrorCode as ErrorCodeEnum,
Severity as SeverityEnum,
Category as CategoryEnum,
RetryStrategy as RetryStrategyEnum,
HardwareWalletError,
} from '@metamask/keyring-utils';
import {
createLedgerError,
isKnownLedgerError,
getLedgerErrorMapping,
} from './errors';
describe('createLedgerError', () => {
it('should create a HardwareWalletError from a known error code', () => {
const error = createLedgerError('0x6985');
expect(error).toBeInstanceOf(HardwareWalletError);
expect(error.message).toContain('User rejected');
expect(error.code).toBe(ErrorCodeEnum.USER_CANCEL_001);
});
it('should create a HardwareWalletError with context', () => {
const error = createLedgerError('0x6985', 'during transaction signing');
expect(error).toBeInstanceOf(HardwareWalletError);
expect(error.message).toContain('User rejected');
expect(error.message).toContain('(during transaction signing)');
});
it('should create a fallback error for unknown error codes without context', () => {
const error = createLedgerError('0x9999');
expect(error).toBeInstanceOf(HardwareWalletError);
expect(error.message).toBe('Unknown Ledger error: 0x9999');
expect(error.code).toBe(ErrorCodeEnum.UNKNOWN_001);
expect(error.severity).toBe(SeverityEnum.ERROR);
expect(error.category).toBe(CategoryEnum.UNKNOWN);
expect(error.retryStrategy).toBe(RetryStrategyEnum.NO_RETRY);
});
it('should create a fallback error for unknown error codes with context', () => {
const error = createLedgerError('0x9999', 'while doing something');
expect(error).toBeInstanceOf(HardwareWalletError);
expect(error.message).toBe(
'Unknown Ledger error: 0x9999 (while doing something)',
);
expect(error.code).toBe(ErrorCodeEnum.UNKNOWN_001);
});
});
describe('isKnownLedgerError', () => {
it('should return true for known error codes', () => {
expect(isKnownLedgerError('0x6985')).toBe(true);
expect(isKnownLedgerError('0x5515')).toBe(true);
expect(isKnownLedgerError('0x6a80')).toBe(true);
});
it('should return false for unknown error codes', () => {
expect(isKnownLedgerError('0x9999')).toBe(false);
expect(isKnownLedgerError('0x0000')).toBe(false);
});
});
describe('getLedgerErrorMapping', () => {
it('should return error mapping for known error codes', () => {
const mapping = getLedgerErrorMapping('0x6985');
expect(mapping).toBeDefined();
expect(mapping?.customCode).toBe(ErrorCodeEnum.USER_CANCEL_001);
expect(mapping?.message).toContain('User rejected');
});
it('should return undefined for unknown error codes', () => {
const mapping = getLedgerErrorMapping('0x9999');
expect(mapping).toBeUndefined();
});
});