js-algorand-sdk
js-algorand-sdk copied to clipboard
Algosdk fails parsing JSON from error response when there is no JSON data in the response.
Subject of the issue
Algosdk fails parsing JSON from error response when there is no JSON data in the response.
Steps to reproduce
It's difficult to reproduce but I can show you exactly where the problem is.
Expected behaviour
My sandbox is returning a 504 status code from this route algod/v2/transactions/params.
When calling algoClient.getTransactionParams().do() it should print a reasonable error message.
Actual behaviour
Algosdk attempts to parse JSON data from the error response when there is no JSON data resulting in the following unreasonable error message:
SyntaxError: Unexpected token E in JSON at position 0
at JSON.parse (<anonymous>)
at Function.parseJSON (client.ts:125:8)
at Function.prepareResponse (client.ts:187:12)
at Function.prepareResponseError (client.ts:209:33)
at HTTPClient.get (client.js:186:30)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
The attempt to parse JSON is made here: https://github.com/algorand/js-algorand-sdk/blob/447816198ada99663dbc1c5cfa8df41d9e5124cd/src/client/client.ts#L126
The problem originates from this request here: https://github.com/algorand/js-algorand-sdk/blob/447816198ada99663dbc1c5cfa8df41d9e5124cd/src/client/client.ts#L233
When the sandbox has a problem (for me it is status 504 at the moment) it throws an error with no JSON in the response which is handled here: https://github.com/algorand/js-algorand-sdk/blob/447816198ada99663dbc1c5cfa8df41d9e5124cd/src/client/client.ts#L233
It tries to parse the response from err.response which is not JSON so it throws the aforementioned error.