janispritzkau / rcon-client

A simple and modern RCON client made to work with Minecraft
64 stars 17 forks source link

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

Open aldldl opened 4 years ago

aldldl commented 4 years ago

@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.

janispritzkau commented 4 years ago

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.

aldldl commented 4 years ago

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.

janispritzkau commented 4 years ago

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.

aldldl commented 4 years ago

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.