-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.test.ts
More file actions
170 lines (145 loc) · 6.15 KB
/
Copy patherrors.test.ts
File metadata and controls
170 lines (145 loc) · 6.15 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { describe, it, expect } from 'vitest';
import {
parseContractError,
normalizeError,
toILNError,
InvalidDiscountRateError,
TokenMismatchError,
PayerReputationTooLowError,
GenericContractError,
InsufficientBalanceError,
NetworkError,
TransactionFailedError,
ValidationError,
WalletNotConnectedError,
SimulationError,
ILNError,
} from './errors';
describe('SDK Structured Error Handling', () => {
it('maps known contract error strings to specific error types', () => {
const errRate = parseContractError('Error: InvalidDiscountRate', 'submit_invoice');
expect(errRate).toBeInstanceOf(InvalidDiscountRateError);
expect(errRate.code).toBe('INVALID_DISCOUNT_RATE');
expect(errRate.context).toMatchObject({
rawError: 'Error: InvalidDiscountRate',
matchedSignature: 'submit_invoice',
matchedPattern: 'InvalidDiscountRate',
});
const errToken = parseContractError('Error: TokenMismatch');
expect(errToken).toBeInstanceOf(TokenMismatchError);
expect(errToken.code).toBe('TOKEN_MISMATCH');
const errPayer = parseContractError('Error: PayerReputationTooLow');
expect(errPayer).toBeInstanceOf(PayerReputationTooLowError);
expect(errPayer.code).toBe('PAYER_REPUTATION_TOO_LOW');
});
it('maps unclassified contract errors to GenericContractError with raw context and signature', () => {
const err = parseContractError('UnknownContractPanic(42)', 'fund_invoice');
expect(err).toBeInstanceOf(GenericContractError);
expect(err.code).toBe('CONTRACT_ERROR');
expect(err.context).toMatchObject({
rawError: 'UnknownContractPanic(42)',
matchedSignature: 'fund_invoice',
matchedPattern: 'Unknown',
});
});
describe('ILNError prototype & structured fields', () => {
it('preserves prototype chain for instanceof checks', () => {
const balanceErr = new InsufficientBalanceError();
const networkErr = new NetworkError();
const txErr = new TransactionFailedError();
const valErr = new ValidationError();
const walletErr = new WalletNotConnectedError();
const simErr = new SimulationError();
expect(balanceErr).toBeInstanceOf(InsufficientBalanceError);
expect(balanceErr).toBeInstanceOf(ILNError);
expect(balanceErr).toBeInstanceOf(Error);
expect(networkErr).toBeInstanceOf(NetworkError);
expect(networkErr).toBeInstanceOf(ILNError);
expect(txErr).toBeInstanceOf(TransactionFailedError);
expect(txErr).toBeInstanceOf(ILNError);
expect(valErr).toBeInstanceOf(ValidationError);
expect(valErr).toBeInstanceOf(ILNError);
expect(walletErr).toBeInstanceOf(WalletNotConnectedError);
expect(walletErr).toBeInstanceOf(ILNError);
expect(simErr).toBeInstanceOf(SimulationError);
expect(simErr).toBeInstanceOf(ILNError);
});
it('has unique programmatic error codes across all SDK error classes', () => {
const errors = [
new InvalidDiscountRateError(),
new TokenMismatchError(),
new PayerReputationTooLowError(),
new InsufficientBalanceError(),
new NetworkError(),
new TransactionFailedError(),
new ValidationError(),
new WalletNotConnectedError(),
new GenericContractError('raw'),
new SimulationError(),
];
const codes = errors.map((e) => e.code);
const uniqueCodes = new Set(codes);
expect(uniqueCodes.size).toBe(errors.length);
});
it('populates docsUrl on all SDK errors', () => {
const errors = [
new InvalidDiscountRateError(),
new TokenMismatchError(),
new PayerReputationTooLowError(),
new InsufficientBalanceError(),
new NetworkError(),
new TransactionFailedError(),
new ValidationError(),
new WalletNotConnectedError(),
new GenericContractError('raw'),
new SimulationError(),
];
for (const err of errors) {
expect(err.docsUrl).toBeDefined();
expect(err.docsUrl).toContain('docs/errors.md#');
expect(err.docsUrl).toContain(err.code.toLowerCase());
}
});
it('supports custom remediation strategies and context propagation', () => {
const customMsg = 'Custom validation error details';
const customRemedy = 'Please enter valid address string';
const ctx = { field: 'discountRate', value: -5 };
const valErr = new ValidationError(customMsg, customRemedy, ctx);
expect(valErr.message).toBe(customMsg);
expect(valErr.remediation).toBe(customRemedy);
expect(valErr.context).toEqual(ctx);
});
});
describe('normalizeError / toILNError', () => {
it('returns an existing ILNError instance unchanged', () => {
const orig = new InsufficientBalanceError();
const normalized = normalizeError(orig);
expect(normalized).toBe(orig);
});
it('wraps standard JS Error objects into ILNError', () => {
const jsErr = new TypeError('Cannot read properties of undefined');
const normalized = normalizeError(jsErr, 'TYPE_ERROR', 'A type error occurred.');
expect(normalized).toBeInstanceOf(ILNError);
expect(normalized.code).toBe('TYPE_ERROR');
expect(normalized.message).toBe('Cannot read properties of undefined');
expect(normalized.cause).toBe(jsErr);
expect(normalized.docsUrl).toContain('docs/errors.md#type_error');
});
it('parses error strings into ILNError using parseContractError', () => {
const normalized = normalizeError('Error: InvalidDiscountRate');
expect(normalized).toBeInstanceOf(InvalidDiscountRateError);
expect(normalized.code).toBe('INVALID_DISCOUNT_RATE');
});
it('wraps arbitrary non-Error values and objects into structured ILNError', () => {
const objErr = { status: 500, detail: 'Internal Gateway Timeout' };
const normalized = normalizeError(objErr, 'GATEWAY_ERROR');
expect(normalized).toBeInstanceOf(ILNError);
expect(normalized.code).toBe('GATEWAY_ERROR');
expect(normalized.context).toBeDefined();
expect(normalized.context?.raw).toEqual(objErr);
});
it('aliases toILNError to normalizeError', () => {
expect(toILNError).toBe(normalizeError);
});
});
});