nazarovsa / node-qiwi-api

Node API to work with QIWI Wallet
MIT License
13 stars 12 forks source link

Wrong error catching method #17

Open AKJee opened 3 years ago

AKJee commented 3 years ago

When doing getTransactionInfo( tId ) - getting unhandled rejection:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'data' of undefined
    at get (path\node_modules\node-qiwi-api\lib\asyncApi.js:724:34)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async init (path\index.js:101:13)

Which goes from this fn:

async function get(options) {
        options.headers = headers;
        try {
            var result = await axios.get(options.url, options);
            console.log(result)
            if (result.data.errorCode != undefined)
                throw result.data;

            return result.data;
        }
        catch (error) {
            throw error.response.data;
        }
    }

However if we check error object getting into catch part - it's completely normal response, with fields:

errorCode: 0,
error: null

So the error in comparing 0 with undefined, getting true here, then throwing error. Line 718 The fix would be like this:

if (result.data.errorCode) {
    //
}

When it's 0 (false) it won't trigger if. Any other err code will.

or

if (result.data.error != null) {
    //
}

Same error check method is used in lines 736 754 771 788

Also comparisons like this error.message != undefined

on lines 703 677

May also cause problem at some point ( or at least it's senseless, because of the same reason described above )

Despite I didn't get any missthrows on these lines by now, I recommend you to look into them more closely and probably change the same way.