@@ -44,6 +44,14 @@ const netModule =
4444const 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+
4755function 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}
0 commit comments