mahdiyari / hive-tx-js

Lightweight and complete JavaScript library for using Hive blockchain in Javascript environments such as Web or NodeJS.
MIT License
25 stars 12 forks source link

Catching Axios and internal library errors - mainly axios #41

Closed Penderis closed 8 months ago

Penderis commented 8 months ago

Hi, I might be missing the boat completely and if I am just doing things wrong tips are welcome. I am not however able to catch the axios errors returned from the library via try catch blocks and the promise catch.

Some are hard to test but this is the general error it will always return and then kill the node instance image

The simplest way to reproduce is to use a wrong trx id on a account_history.get_transaction call

try{
        console.log('test trx',req.params.trx)
        hive.call('account_history_api.get_transaction',[req.params.trx])
                            .then(result=> res.send(result))
                            .catch((err)=>{
                                console.log('promise catch',err)
                                throw new Error('f*#@ axios')
                            })       
    }catch(e){
        console.log('try catch',e)
        res.send(e)        
    }

but it would be the same for trying to use Privatekey.from on a wrong key. I am not sure how to catch these errors to avoid them crashing the instance.

mahdiyari commented 8 months ago

Are you sure your error is from this? An RPC error for wrong trx_id wouldn't evoke an error to begin with. It will be a normal result because the API will answer with a message.

I suspect the error is from something else that you are not catching.

mahdiyari commented 8 months ago

BTW I did write the above comment after testing your posted code.

mahdiyari commented 8 months ago

Also your format of the API call is wrong. Almost all the API methods other than condenser_api require an object input and not an array. And API node will return an error telling you this. You can replace account_history_api with condenser_api for quick fix.

mahdiyari commented 8 months ago

Also, the "try catch" won't catch the error thrown in the promise catch in this code. You have to use await/async for your "try catch" to catch the errors from that promise. Try & catch on a promise won't catch the errors happening later without using await/async. That's why there is .catch() for non async codes.

Penderis commented 8 months ago

Also your format of the API call is wrong. Almost all the API methods other than condenser_api require an object input and not an array. And API node will return an error telling you this. You can replace account_history_api with condenser_api for quick fix.

It works with that although I did notice I use an object in other ones, not sure how settled on that one but yeah it works. I did run test now using object id:trx and still returns result if correct TRX id. I will test with condenser also. I merely used this one as the simplest way to reproduce the issue.

to be clear the only time I get the uncaught exception is if I expect a rpc error to be returned. But as mentioned in other comment will do more testing or just use fetch where possible. Thank you.

Penderis commented 8 months ago

Also, the "try catch" won't catch the error thrown in the promise catch in this code. You have to use await/async for your "try catch" to catch the errors from that promise. Try & catch on a promise won't catch the errors happening later without using await/async. That's why there is .catch() for non async codes.

Thank you, I kinda did not click that but the catch from the .then format should though catch the error? I did change it from await just to have both catches there without thinking that it would be pointless. I will setup an await one only for testing.

Penderis commented 8 months ago

Are you sure your error is from this? An RPC error for wrong trx_id wouldn't evoke an error to begin with. It will be a normal result because the API will answer with a message.

I suspect the error is from something else that you are not catching.

This could be then from express , although I only get it with hive-tx calls , for thoroughness I will test using fetch directly which I know returns me the rpc error if there is one also but exact call to see which fails. Thank you, I will look into it then sorry for the hassle.