forked from Echo-Mirror-Butler/echomirror-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.test.ts
More file actions
150 lines (134 loc) · 5.44 KB
/
Copy patherrors.test.ts
File metadata and controls
150 lines (134 loc) · 5.44 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
import { describe, expect, it } from 'vitest'
import {
AccountNotFoundError,
BadSequenceError,
HorizonRateLimitError,
HorizonTimeoutError,
HorizonUnavailableError,
InsufficientBalanceError,
InsufficientFeeError,
PathNotFoundError,
TransactionExpiredError,
TransactionMalformedError,
TrustlineMissingError,
WalletConnectionError,
WalletUserRejectedError,
mapHorizonError,
mapWalletError,
} from '../src/errors'
/** Fake the axios-style error shape stellar-sdk attaches Horizon responses to. */
function horizonError(status: number, resultCodes?: { transaction?: string; operations?: string[] }, headers?: Record<string, string>) {
return {
response: {
status,
headers,
data: {
status,
title: 'Transaction Failed',
extras: resultCodes ? { result_codes: resultCodes } : undefined,
},
},
}
}
describe('mapHorizonError', () => {
it('maps 504 to a retryable timeout', () => {
const err = mapHorizonError(horizonError(504))
expect(err).toBeInstanceOf(HorizonTimeoutError)
expect(err.retryable).toBe(true)
})
it('maps 429 to a retryable rate limit honouring retry-after', () => {
const err = mapHorizonError(horizonError(429, undefined, { 'retry-after': '17' }))
expect(err).toBeInstanceOf(HorizonRateLimitError)
expect((err as HorizonRateLimitError).retryAfterSeconds).toBe(17)
expect(err.retryable).toBe(true)
})
it('maps 5xx to retryable unavailability', () => {
const err = mapHorizonError(horizonError(503))
expect(err).toBeInstanceOf(HorizonUnavailableError)
expect(err.retryable).toBe(true)
})
it('maps fetch-level failures to retryable unavailability', () => {
const err = mapHorizonError(new TypeError('fetch failed'))
expect(err).toBeInstanceOf(HorizonUnavailableError)
expect(err.retryable).toBe(true)
})
it.each([
['tx_bad_seq', BadSequenceError],
['tx_too_late', TransactionExpiredError],
['tx_insufficient_fee', InsufficientFeeError],
['tx_insufficient_balance', InsufficientBalanceError],
['tx_malformed', TransactionMalformedError],
['tx_bad_auth', TransactionMalformedError],
])('maps %s to a permanent typed error', (code, cls) => {
const err = mapHorizonError(horizonError(400, { transaction: code }))
expect(err).toBeInstanceOf(cls)
expect(err.retryable).toBe(false)
})
it.each([
['op_underfunded', InsufficientBalanceError],
['op_low_reserve', InsufficientBalanceError],
['op_no_trust', TrustlineMissingError],
['op_src_no_trust', TrustlineMissingError],
['op_no_issuer', TrustlineMissingError],
['op_too_few_offers', PathNotFoundError],
['op_over_source_max', PathNotFoundError],
['op_under_dest_min', PathNotFoundError],
])('maps tx_failed + %s to a permanent typed error', (op, cls) => {
const err = mapHorizonError(horizonError(400, { transaction: 'tx_failed', operations: [op] }))
expect(err).toBeInstanceOf(cls)
expect(err.retryable).toBe(false)
expect(err.resultCodes?.operations).toContain(op)
})
it('maps op_no_destination to DESTINATION_NOT_FOUND with actionable message', () => {
const err = mapHorizonError(horizonError(400, { transaction: 'tx_failed', operations: ['op_no_destination'] }))
expect(err.code).toBe('DESTINATION_NOT_FOUND')
expect(err.message).toMatch(/createDestination/)
expect(err.retryable).toBe(false)
})
it('keeps unknown operation codes in a generic non-retryable failure', () => {
const err = mapHorizonError(horizonError(400, { transaction: 'tx_failed', operations: ['op_cross_self'] }))
expect(err.code).toBe('TX_FAILED')
expect(err.message).toContain('op_cross_self')
expect(err.retryable).toBe(false)
})
it('maps call-builder errors, whose problem doc sits directly on response', () => {
// stellar-sdk NotFoundError (loadAccount, paths, …) has no response.data:
const err = mapHorizonError({
response: { type: 'https://stellar.org/horizon-errors/not_found', title: 'Resource Missing', status: 404, detail: '…' },
})
expect(err.code).toBe('ACCOUNT_NOT_FOUND')
expect(err.retryable).toBe(false)
const transient = mapHorizonError({
response: { title: 'Internal Server Error', status: 500, detail: '…' },
})
expect(transient.retryable).toBe(true)
})
it('passes through errors that are already typed', () => {
const original = new AccountNotFoundError('GABC', 'destination')
expect(mapHorizonError(original)).toBe(original)
})
})
describe('mapWalletError', () => {
it('detects Albedo rejection by numeric code -4', () => {
const err = mapWalletError('albedo', { code: -4, message: 'Action rejected by user' })
expect(err).toBeInstanceOf(WalletUserRejectedError)
})
it.each([
'User declined access',
'The user rejected this transaction',
'Request was denied',
'cancelled by user',
])('detects rejection message "%s"', (message) => {
const err = mapWalletError('freighter', new Error(message))
expect(err).toBeInstanceOf(WalletUserRejectedError)
})
it('detects rejections thrown as bare strings (xBull style)', () => {
const err = mapWalletError('xbull', 'Connection denied')
expect(err).toBeInstanceOf(WalletUserRejectedError)
})
it('wraps everything else in WalletConnectionError', () => {
const err = mapWalletError('freighter', new Error('extension context invalidated'))
expect(err).toBeInstanceOf(WalletConnectionError)
expect(err.message).toContain('freighter')
})
})