rcon-client icon indicating copy to clipboard operation
rcon-client copied to clipboard

Error on connection not being found when using .on("error")

Open aldldl opened this issue 3 years ago • 4 comments

@janispritzkau I'm not crashing (thanks for the fix), but I can't seem to pull out the error .on("error") never pulls up for me when there is a server that is not on. I've tried a similar process as midforger did above and also had no luck catching the connection error. I wrote this to error #9 but realized I replied after it was fully closed so I wasn't sure if you would see it.

aldldl avatar Sep 24 '20 21:09 aldldl

This was introduced as a side effect of the issue #9 fix but you can still catch connection errors in the promise returned by calling connect().

I'm still considering to rethink some design decisions to make maintaining the library easier and reduce the possibility of bugs. I haven't used a rcon library for a long time and therefore I can't spend too much time on it.

janispritzkau avatar Sep 25 '20 15:09 janispritzkau

I am still getting the following in the log

25|infoBot | I am ready! 25|infoBot | connected 25|infoBot | authenticated 25|infoBot | Rocn: 25|infoBot | end 25|infoBot | Errorconnect ECONNREFUSED 127.0.0.1:34236 25|infoBot | This normally means the server is offline 25|infoBot | You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection: 25|infoBot | Error: Not connected 25|infoBot | at Rcon.sendRaw (/opt/info-bot/node_modules/rcon-client/lib/rcon.js:102:19) 25|infoBot | at Rcon.send (/opt/info-bot/node_modules/rcon-client/lib/rcon.js:97:36) 25|infoBot | at main (/opt/info-bot/commands/kick.js:81:36) 25|infoBot | at processTicksAndRejections (internal/process/task_queues.js:97:5)

I am using this code:

rcon.on("connect", () => console.log("connected"));
    rcon.on("authenticated", () => console.log("authenticated"));
    rcon.on("error", () => console.log("errors"));
    rcon.on("end", () => console.log("end"));
                    
    try{ await rcon.connect(); 
        } catch (error) { msg.channel.send("Error:*"+error.message+"* "+"This normally means you are trying to reach an offline server").then(console.log(error.name + error.message)).then(console.log('This normally means the server is offline'))
        }
        
    const responses = await rcon.send(`/kick ${toKick} ${reason1}`);

    rcon.end();

I get the proper error and text sent back, but still get the warning/issue listed in the log about unhandled promise rejection.

aldldl avatar Sep 25 '20 18:09 aldldl

In your example the error is coming from the send method. You are catching the connect error, but since `rcon.send' is called after the try catch statement, it is called regardless of whether the connection was successful.

janispritzkau avatar Sep 25 '20 19:09 janispritzkau

Oops, Thanks, After getting the errors before I didn't even notice the change when adding the catch to only the connect. I've put it all inside the try and its working as planned.

aldldl avatar Sep 25 '20 20:09 aldldl