From 851d4c404cebd6b7f35919593879a34d93f9f7a0 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 8 Aug 2021 19:51:01 -0700 Subject: [PATCH] fix(rate limits): Fixing more bugs with rate limit handling axios.options --- src/raw-transactions.js | 31 +++++++++++++++++++++++++------ src/slp/utils.js | 19 ++++++++++++++----- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/raw-transactions.js b/src/raw-transactions.js index c631596..0fb9b47 100644 --- a/src/raw-transactions.js +++ b/src/raw-transactions.js @@ -259,6 +259,10 @@ class RawTransactions { async getRawTransaction (txid, verbose = false, usrObj = null) { try { if (typeof txid === 'string') { + console.log( + 'getRawTransaction() this.axiosOptions: ', + this.axiosOptions + ) const response = await axios.get( `${this.restURL}rawtransactions/getRawTransaction/${txid}?verbose=${verbose}`, this.axiosOptions @@ -284,8 +288,13 @@ class RawTransactions { throw new Error('Input must be a string or array of strings.') } catch (error) { if (error.error) throw new Error(error.error) - else if (error.response && error.response.data) throw error.response.data - else throw error + + // This case handles rate limit errors. + if (error.response && error.response.data && error.response.data.error) { + throw new Error(error.response.data.error) + } else if (error.response && error.response.data) { + throw error.response.data + } else throw error } } @@ -330,8 +339,13 @@ class RawTransactions { return retArray } catch (error) { if (error.error) throw new Error(error.error) - else if (error.response && error.response.data) throw error.response.data - else throw error + + // This case handles rate limit errors. + if (error.response && error.response.data && error.response.data.error) { + throw new Error(error.response.data.error) + } else if (error.response && error.response.data) { + throw error.response.data + } else throw error } } @@ -386,8 +400,13 @@ class RawTransactions { return txDetails } catch (error) { if (error.error) throw new Error(error.error) - else if (error.response && error.response.data) throw error.response.data - else throw error + + // This case handles rate limit errors. + if (error.response && error.response.data && error.response.data.error) { + throw new Error(error.response.data.error) + } else if (error.response && error.response.data) { + throw error.response.data + } else throw error } } diff --git a/src/slp/utils.js b/src/slp/utils.js index 85485c5..3147f8f 100644 --- a/src/slp/utils.js +++ b/src/slp/utils.js @@ -450,13 +450,14 @@ class Utils { else txids = txid try { + console.log('validateTxid() this.axiosOptions: ', this.axiosOptions) const response = await _this.axios.post( path, { txids: txids, usrObj // pass user data when making an internal call. }, - _this.axiosOptions + this.axiosOptions ) // console.log( // `validateTxid response.data: ${JSON.stringify(response.data, null, 2)}` @@ -520,7 +521,8 @@ class Utils { const path = `${this.restURL}slp/validateTxid2/${txid}` - const response = await _this.axios.get(path, _this.axiosOptions) + console.log('validateTxid2() this.axiosOptions: ', this.axiosOptions) + const response = await _this.axios.get(path, this.axiosOptions) return response.data } catch (error) { if (error.response && error.response.data) throw error.response.data @@ -666,13 +668,14 @@ class Utils { else txids = txid try { + console.log('validateTxid3() this.axiosOptions: ', this.axiosOptions) const response = await _this.axios.post( path, { txids: txids, usrObj // pass user data when making an internal call. }, - _this.axiosOptions + this.axiosOptions ) // console.log(`response.data: ${JSON.stringify(response.data, null, 2)}`) @@ -967,14 +970,15 @@ class Utils { // CT: 2/24/21 Deprected GET in favor of POST, to pass IP address. // Retrieve the transaction object from the full node. const path = `${this.restURL}rawtransactions/getRawTransaction` - const response = await _this.axios.post( + console.log('decodeOpReturn() this.axiosOptions: ', this.axiosOptions) + const response = await this.axios.post( path, { verbose: true, txids: [txid], usrObj // pass user data when making an internal call. }, - _this.axiosOptions + this.axiosOptions ) const txDetails = response.data[0] // console.log(`txDetails: ${JSON.stringify(txDetails, null, 2)}`) @@ -1647,6 +1651,11 @@ class Utils { // could not be validated. return isValid } catch (error) { + // This case handles rate limit errors. + if (error.response && error.response.data && error.response.data.error) { + throw new Error(error.response.data.error) + } + // console.log('Error in waterfallValidateTxid()') if (error.response && error.response.data) throw error.response.data throw error