Sending error in a specific channel Discord.js - javascript

I have a try...catch system to send an error to a channel when there is an error.
Here is the code:
try {
cmd.execute(client, message, args, prefix, result);
} catch (err) {
echannel.send(err);
}
echannel is the channel that I got with client.channels.cache.get(). The channel is the correct one.
I made a command to throw the error with:
message.channel.send('Throwing error...');
throw new Error('Testing');
I can see the error in the console though. Any help?

Try to simply send the error to the channel:
message.channel.send(err);

I've figured it out. I need to add process.on('uncaughtException, err => {})

Related

Node.js and Zaproxy UnhandledPromiseRejectionWarning: RequestError: Error: socket hang up

var zapClient = require('zaproxy');
const zapOptions = {
key : 'abcdefghijklmn',
proxy : 'http://localhost:8090/'
};
const zaproxy = new zapClient(zapOptions);
zaproxy.spider.scan("https://www.google.co.in");
i am trying to run above code i am getting :
(node:8380) UnhandledPromiseRejectionWarning: RequestError: Error: socket hang up error.
i have tried socket hang up error with nodejs and "Request error: Socket hang up" with nodeJS on Amazon EC2 didn't help.
I believe changing the last line of your code to this should fix the problem:
zaproxy.spider.scan("https://www.google.co.in", (err, res) => {
if (err) throw err;
//do something with res here
});
//can't do anything with res here
Also check the zap.log file - that might be logging errors which by default are not reported to the client (for security reasons - this can be disabled for testing)

How do I send a custom error message from Node, and display it with catch?

I'm trying to send a custom error message from node, receive it on the frontend, and display it.
On server I'm trying to do:
res.status(500).send('Something broke!')
And on client I'm doing:
try {
const res = await axios.post(`${API_URL}/profiles/signup`, credentials)
} catch(err) {
console.log("Sign up error:", err)
}
What I want is to get something like this in my console:
Sign up error: Something broke!
but I'm getting:
Sign up error: Error: Request failed with status code 500
at createError (createError.js:16)
at settle (settle.js:18)
at XMLHttpRequest.handleLoad (xhr.js:77)
How do I access the custom message I'm sending?
You can create an error with the help of Error()
throw new Error({code: 500, message: 'Sign up error: Something broke!'});
throw send your error to the catch block automatically.
Figured it out:
console.log("Sign up error:", err.response.data)

Cannot catch UnhandledPromiseRejectionWarning in promise

so I'm trying to catch UnhandledPromiseRejectionWarning in my promise, but for some reason it's not working. It ignores my code and just outputs the error to console.
Error:
UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 1): Error: Forbidden (Cannot send messages to this
user)
code:
e.message.author.openDM().then((message) => {
message.sendMessage(`test`);
}).catch((error) => {
e.message.channel.sendMessage(error + "test");
});
This is a discord bot, using discordie. In my mind, the above code should send the word "test" to a messages author via private message, if the bot can't, it will send the error and the word test in the channel they sent their message in. However, the second part (inside the catch) doesn't get executed.
tl;dr, The catch in the above code isn't working and I'm getting the above error in console instead if the bot doesn't have permission to dm the user.
You forgot the return statement inside the then function.
I suppose message.sendMessage('test') returns a promise
e.message.author.openDM().then((message) => {
return message.sendMessage(`test`);
}).catch((error) => {
e.message.channel.sendMessage(error + "test");
});

Get more info about uncaughtException in NodeJS

How do i get more info on events like unhandledError or unhandledRejection in nodeJs?
For example, in browser, i can simply add this listener:
window.onerror = function(message, page, lineno, colno, error) {
};
Which gives me error name, stack, type and even location.
But in nodeJs, i see only the message, and nothing else:
process.on('uncaughtException', (err) => {
console.log('catched!', err);
});
How do i get more info about it? full stack trace, type of error separately from it's message etc? Like i did in browser.

node writeFileSync not writing to file

Here is the command that is broken:
fs.writeFileSync('Metadata.json', metadataString);
console.log("Metadata written.");
I have placed a breakpoint and verified that metadataString is actually a string. The console logs Metadata written just fine and the file stays empty...
Not sure what else I can check... Thanks in advance.
fs.writeFileSync() throws an Error exception if it fails. Use a try/catch block:
try {
fs.writeFileSync('Metadata.json', metadataString);
} catch (err) {
console.log('Error writing Metadata.json:' + err.message)
}

Categories

Resources