MiniGod / node-gbxremote

JavaScript port of GbxRemote to communicate with ManiaPlanet / TrackMania servers with Node.js.
MIT License
11 stars 4 forks source link

UnhandledPromiseRejectionWarning #5

Closed gastg77 closed 5 years ago

gastg77 commented 6 years ago

Hi,

I'm facing with what I will call an issue with your gbxremote's lib.

First of all, I'd like to say that I'm not familiar with promises. I understand the theory but never coded with it.

I have this code :

const gbx = require("gbxremote");

module.exports = {
    setServerName: setServerName
};

/*
 * - EXPORTED FUNCTION -
 * Set the server's name to a server
 * @param {string} host
 * @param {integer} xmlrpc_port
 * @param {string} admin_password
 * @param {string} server_name
 * @param {function} cb
 * @returns {callback}
 */

function setServerName(host, xmlrpc_port, admin_password, server_name, cb) {
    const client = gbx.createClient(xmlrpc_port, host);

    client.on('connect', function () {
        client.query('Authenticate', ['Admin', admin_password]).then(function (res) {
            if (res === true) {
                // Set the name of the server
                client.query('SetServerName', server_name).then(function (res) {
                    if (typeof cb === 'undefined') {
                        return cb(null, true);
                    }
                }).catch(function (err) {
                    if (typeof cb === 'undefined') {
                        return cb("Can't change the server name", true);
                    }
                });
            }
        }).catch(function (err) {
            if (typeof cb === 'undefined') {
                return cb("Authent err: " + err, true);
            }
        });
    }).on("error", function (err) {
        if (typeof cb === 'undefined') {
            return cb("Err: " + err, true);
        }
    });
}

When I execute it here is what I have :

Err: Error: getaddrinfo ENOTFOUND trackmania_1 trackmania_1:undefined ## --- console message display with my code
(node:2298) UnhandledPromiseRejectionWarning: undefined
(node:2298) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:2298) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

My server is not launch. I wanted to test this case (it can be for another reason, like the "xmlrpc_allowremote" parameter set to 'false') and it is not what I expected.

Did I miss something in my code or is it really an issue ?

Thanks !

Seb

MiniGod commented 6 years ago

Hi @gastg77. Did you figure this out?

If there is an error inside your catch handler, I think you would get that error.

Looking at the code, I see you're doing if (typeof cb === 'undefined') and then calling cb... That means you're calling cb only when it is undefined, which will not work.

If that's not the issue, could you please run node with --trace-warnings and provide the trace?

gastg77 commented 6 years ago

Hey @MiniGod

Thanks for reviewing my code, I did not see the mistake :) (I'll check my code right away)

Actually, I did it another way. Before calling the function that use your lib, I test if the server is launched or not. If it is, then I call the function. Not very clean because all the error that can happen won't be catch but .... I'll check your fix !

Thanks !