diff --git a/src/slp/utils.js b/src/slp/utils.js index bd70131..0245ade 100644 --- a/src/slp/utils.js +++ b/src/slp/utils.js @@ -1,3 +1,5 @@ +/* eslint-disable no-useless-catch */ + // Public npm libraries const axios = require('axios') const slpParser = require('slp-parser') @@ -1014,21 +1016,16 @@ class Utils { return tokenData } catch (error) { - console.log('decodeOpReturn error: ', error) - console.log(`decodeOpReturn error.message: ${error.message}`) - - if (error.response && error.response.data) { - // throw error.response.data - - // error.bchApiMessage = error.message - // error.message = error.response.data - // error.data = error.response.data - console.log( - `decodeOpReturn error.response.data: ${JSON.stringify( - error.response.data - )}` - ) - } + // Used for debugging + // console.log('decodeOpReturn error: ', error) + // console.log(`decodeOpReturn error.message: ${error.message}`) + // if (error.response && error.response.data) { + // console.log( + // `decodeOpReturn error.response.data: ${JSON.stringify( + // error.response.data + // )}` + // ) + // } throw error } } diff --git a/test/unit/fixtures/slp/mock-utils.js b/test/unit/fixtures/slp/mock-utils.js index c37fd18..a5d672e 100644 --- a/test/unit/fixtures/slp/mock-utils.js +++ b/test/unit/fixtures/slp/mock-utils.js @@ -1366,6 +1366,23 @@ const txDetailsSLPNftChild = { } const mock429Error = new Error('Request failed with status code 429') +mock429Error.response = { + status: 429, + statusText: 'Too Many Requests', + data: { + error: + 'Too many requests. Your limits are currently 100 requests per minute. Increase rate limits at https://fullstack.cash' + } +} + +const mock503Error = new Error('Request failed with status code 503') +mock503Error.response = { + status: 503, + statusText: 'Service Unavailable', + data: { + error: 'Some error message' + } +} module.exports = { mockList, @@ -1399,5 +1416,6 @@ module.exports = { whitelist, slpdbStatus, mockValidateTxidArray, - mock429Error + mock429Error, + mock503Error } diff --git a/test/unit/slp-utils.js b/test/unit/slp-utils.js index c93cb55..65832dc 100644 --- a/test/unit/slp-utils.js +++ b/test/unit/slp-utils.js @@ -495,8 +495,37 @@ describe('#SLP Utils', () => { assert.fail('Unexpect result') } catch (err) { - console.log('err: ', err) - // assert.include(err.message, 'amount string size not 8 bytes') + // console.log('err: ', err) + + assert.property(err, 'message') + assert.property(err, 'response') + assert.equal(err.response.status, 429) + assert.equal(err.response.statusText, 'Too Many Requests') + assert.include(err.response.data.error, 'Too many requests') + } + }) + + // 503 means the service is down. + it('should throw error when axios returns 503 status', async () => { + try { + // Mock the call to the REST API + sandbox.stub(uut.Utils.axios, 'post').rejects(mockData.mock503Error) + + const txid = + 'a60a522cc11ad7011b74e57fbabbd99296e4b9346bcb175dcf84efb737030415' + + await uut.Utils.decodeOpReturn(txid) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.fail('Unexpect result') + } catch (err) { + // console.log('err: ', err) + + assert.property(err, 'message') + assert.property(err, 'response') + assert.equal(err.response.status, 503) + assert.equal(err.response.statusText, 'Service Unavailable') + assert.property(err.response, 'data') } }) })