discord rpc application login unsuccessful error

405 Views Asked by At

I had a issue with my app shutting down when discord wasn't launched on desktop because of RPC.

rpc.login({
    clientId: "clientid"
})
.catch(console.error); 

Now when I added the catch block it doesn't shut down when it cannot connect but it shows a pretty long error to the user, which I would like to hide or replace with a smaller console log like "RPC offline" or something

Error: Could not connect
at Socket.onerror (C:\snapshot\test\node_modules\discord-rpc\src\transports\ipc.js:32:16)
at Object.onceWrapper (node:events:514:26)
at Socket.emit (node:events:394:28)
at emitErrorNT (node:internal/streams/destroy:193:8)
at emitErrorCloseNT (node:internal/streams/destroy:158:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)

ofcourse i tried to use normal try-catch and only .catch(error) instead of .catch(console.error) but that doesn't seem to catch the unsuccessful connection and just shuts down the script again.

2

There are 2 best solutions below

0
On BEST ANSWER

The rpc.login method returns a Promise which if fails to fulfil ( rejected promise ) throws an error, your approach was absolutely correct to use Promise.prototype.catch & try/catch and from what I can understand by your question

I would like to hide or replace with a smaller console log like "RPC offline".

You can simply pass a console.log statement when you catch the error in your promise like so:

rpc.login({
    clientId: "clientid"
}).catch((e) => {
    console.log(`RPC Offline`)
});

Or use try/catch/finally - ( Suggested if retrying to login multiple times )


try {
    login(rpc);
}
catch (e) {
    console.log(`RPC Offline`)
}
finally {
    console.log(`Could not connect therefore retrying to login`)
    login(rpc);
}


async function login(rpc) {
    await rpc.login({
        clientId: "clientid"
    })
}

0
On

Now when I added the catch block it doesn't shut down when it cannot connect but it shows a pretty long error to the user, which I would like to hide or replace with a smaller console log like "RPC offline" or something

You mean you want to do something like .catch(() => console.log("RPC Offline"))?

ofcourse i tried to use normal try-catch and only .catch(error) instead of .catch(console.error) but that doesn't seem to catch the unsuccessful connection and just shuts down the script again.

Because it's a promise and you can't handle rejections via try-catch unless you are awaiting