Skip to content

Commit 2dc06e5

Browse files
authored
Merge pull request #7 from bitcoinerlab/normalize-error-throw
fix: normalize error handling and update tests
2 parents 3ed970e + 2c84025 commit 2dc06e5

File tree

5 files changed

+38
-25
lines changed

5 files changed

+38
-25
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@bitcoinerlab/explorer",
33
"description": "Bitcoin Blockchain Explorer: Client Interface featuring Esplora and Electrum Implementations.",
44
"homepage": "https://github.com/bitcoinerlab/explorer",
5-
"version": "0.2.0",
5+
"version": "0.2.1",
66
"author": "Jose-Luis Landabaso",
77
"license": "MIT",
88
"prettier": "@bitcoinerlab/configs/prettierConfig.json",

src/electrum.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ const netModule =
4444
const tlsModule =
4545
typeof global !== 'undefined' && global.tls ? global.tls : tls;
4646

47+
function getErrorMsg(error: unknown): string {
48+
if (typeof error === 'string') {
49+
return error;
50+
} else {
51+
return (error as Error).message;
52+
}
53+
}
54+
4755
function defaultElectrumServer(network: Network = networks.bitcoin): {
4856
host: string;
4957
port: number;
@@ -167,9 +175,8 @@ export class ElectrumExplorer implements Explorer {
167175
);
168176
const header = await this.#client.blockchainHeaders_subscribe();
169177
this.#updateBlockTipHeight(header);
170-
} catch (_error: unknown) {
171-
const error = _error as Error;
172-
throw new Error(`Failed to init Electrum: ${error.message}`);
178+
} catch (error: unknown) {
179+
throw new Error(`Failed to init Electrum: ${getErrorMsg(error)}`);
173180
}
174181

175182
// Ping every minute to keep connection alive. Reconnect on error.
@@ -181,11 +188,13 @@ export class ElectrumExplorer implements Explorer {
181188
}
182189
try {
183190
await this.#client.server_ping();
184-
} catch (_error: unknown) {
185-
const error = _error as Error;
191+
} catch (error: unknown) {
186192
// Ping failed, stop pinging and reconnect
187193
await this.close();
188-
console.warn('Reconnecting in 0.5s after ping error:', error.message);
194+
console.warn(
195+
'Reconnecting in 0.5s after ping error:',
196+
getErrorMsg(error)
197+
);
189198
await new Promise(resolve => setTimeout(resolve, 500));
190199
await this.connect();
191200
}
@@ -302,10 +311,11 @@ export class ElectrumExplorer implements Explorer {
302311
*/
303312
client = await this.#getClient();
304313
history = await client.blockchainScripthash_getHistory(scriptHash);
305-
} catch (_error: unknown) {
306-
const error = _error as Error;
314+
} catch (error: unknown) {
307315
throw new Error(
308-
`Failed getting balance & history of ${scriptHash}: ${error.message}`
316+
`Failed getting balance & history of ${scriptHash}: ${getErrorMsg(
317+
error
318+
)}`
309319
);
310320
}
311321
const _txCount = history.filter(tx => tx.height > 0).length;
@@ -333,9 +343,8 @@ export class ElectrumExplorer implements Explorer {
333343
const client = await this.#getClient();
334344
const fee = await client.blockchainEstimatefee(target);
335345
feeEstimates[target] = 100000 * fee;
336-
} catch (_error: unknown) {
337-
const error = _error as Error;
338-
throw new Error(`Failed to fetch fee estimates: ${error.message}`);
346+
} catch (error: unknown) {
347+
throw new Error(`Failed to fetch fee estimates: ${getErrorMsg(error)}`);
339348
}
340349
}
341350
checkFeeEstimates(feeEstimates);
@@ -381,12 +390,11 @@ export class ElectrumExplorer implements Explorer {
381390
try {
382391
const client = await this.#getClient();
383392
history = await client.blockchainScripthash_getHistory(scriptHash);
384-
} catch (_error: unknown) {
385-
const error = _error as Error;
393+
} catch (error: unknown) {
386394
throw new Error(
387395
`Failed to fetch transaction history for address/scriptHash "${
388396
scriptHash || address
389-
}": ${error.message}`
397+
}": ${getErrorMsg(error)}`
390398
);
391399
}
392400
if (history.length > this.#maxTxPerScriptPubKey)
@@ -422,10 +430,9 @@ export class ElectrumExplorer implements Explorer {
422430
try {
423431
const client = await this.#getClient();
424432
return await client.blockchainTransaction_get(txId);
425-
} catch (_error: unknown) {
426-
const error = _error as Error;
433+
} catch (error: unknown) {
427434
throw new Error(
428-
`Failed to fetch transaction tx for "${txId}": ${error.message}`
435+
`Failed to fetch transaction tx for "${txId}": ${getErrorMsg(error)}`
429436
);
430437
}
431438
}
@@ -449,9 +456,8 @@ export class ElectrumExplorer implements Explorer {
449456
}
450457

451458
return txId;
452-
} catch (_error: unknown) {
453-
const error = _error as Error;
454-
throw new Error(`Failed to broadcast transaction: ${error.message}`);
459+
} catch (error: unknown) {
460+
throw new Error(`Failed to broadcast transaction: ${getErrorMsg(error)}`);
455461
}
456462
}
457463
}

src/esplora.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async function esploraFetch(
2020
if (!response.ok) {
2121
const errorDetails = await response.text();
2222
throw new Error(
23-
`Network request failed! Status code: ${response.status} (${response.statusText}). URL: ${response.url}. Server response: ${errorDetails}`
23+
`Failed request: ${errorDetails}. Status code: ${response.status} (${response.statusText}). URL: ${response.url}.`
2424
);
2525
}
2626
return response;

test/explorer.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ for (const regtestExplorer of regtestExplorers) {
211211
expect(blockStatus?.blockHash).toBe(header.getId());
212212
expect(blockStatus?.blockHeight).toBe(tipHeight);
213213
});
214+
test(`Push errors`, async () => {
215+
await expect(
216+
explorer.push(
217+
'02000000000101a181e1db4f39ca9b14d764373d919bb01bdaa771a39d61bfc45e4c73b33b6bee00000000171600146a166349647f19f776f9f8c4dc8604b36b854ae4ffffffff02a08601000000000017a914a44bb6bc3228487f4a120fd187b13c23a4886cfc878a0035000000000017a91490bb10362943c1dc6bc97bae807525e934fa9a898702483045022100e4b56cf9ea496604d4ecb9898b7b221a3237e6c9eb12bc1473cf6946ac9244390220215e54c0e3d1131088a56caeac2b21099f33317b865affaef22ee6f3b7ea37980121031dd46bf77dd63d55bab8209dd606b16167d8c75a663475cc5abf85deb64a565600000000'
218+
) //Push a problematic tx that has "Missing inputs"
219+
).rejects.toThrow(/bad-txns-inputs-missingorspent/);
220+
});
214221
test('close', async () => {
215222
await explorer.close();
216223
});

0 commit comments

Comments
 (0)