I don't know exactly how to use catch err, send a message instead of giving an error when it blocks the bot in the private message I want to do.
This code:
try{
message.member.send("trying private message.")
} catch(e) {
message.channel.send("Your private message is closed.");
};
When I get an error the catch err part doesn't work.
Sending a message returns a promise. You can handle the promise error by using the catch() method.
message.member.send('Trying to send a DM!')
.catch(error => {
console.log(error);
message.channel.send('Your DMs are closed.');
});
Related
I have a front-end connected to back-end, using http requests.
I'm experiencing this thing where if I return res.status(200) I get a response which I can send with that response a message. But when I'm sending res.status(400). I'm getting an red error on console rather than a parsable response.
app.post('/new', (req, res) => {
const { fullname, phone, country, img } = req.body
return res.status(200).json({
title: 'Ok, we got you!'
})
})
// If I'm changing that status to 400 I get that red error on console and I cannot parse from it anything (anything atleast that I want)
Yes, and that's the correct behaviour with HTTP requests. HTTP 400 Bad Request indicates an error rather then a successful response from your web server. So you need to catch the error thrown by the server with either a .catch() clause or a try/catch block in order to access the data returned on your client side. You're server code is absolutely fine. Just add the code below to your client side application.
Using .catch approach
axios.get('/foo')
.catch(function(error) {
console.log(error.response.data);
console.log(error.response.data.title); // your title
console.log(error.response.status);
console.log(error.response.headers);
});
Using try/catch approach
try {
const resp = await axios.get('/foo');
} catch (error) {
console.log(error.response.data);
console.log(error.response.data.title); // your title
console.log(error.response.status);
console.log(error.response.headers);
}
For more information visit this link
I have a problem to get my custom error message from the server.
I'm using axios to make the call and Express as a server.
I got the status of the error but don't success to get the message only the status.
Express-Server response
res.status(420).json({ message: "some problem" });
react axios call
export function* deleteItem(action) {
try {
yield put(actions.deleteItemStart());
yield axios.delete("/items/item/" + action.itemId);
yield put(actions.deleteItemSuccess());
} catch (error) {
console.log(error);
yield put(actions.deleteItemFailed(error));
}
}
this the error I get without the message
Error: Request failed with status code 420
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:61)
this is my first post I hope it's all clear.
thanks in advance Gilad :)
actually you can get axios errors with
catch(e){
console.log("error response:", e.response);
}
e.response contains the server response if there is one.
I want it so my bot can only send embeds in specific channels with the permissions granted but if a user sends the message in a channel where the permissions are not granted the bot stops working and cuts off. I am trying to make it so a try catch works so even if an error is thrown it continues to work, the error I am getting is "UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Permissions - I want to be able to ignore this error.
function catchErr (err, emessage, a){
console.log("Someone tried to use the embed in the");}
let args = message.content.slice(PREFIX.length).split(' ');
switch(args[0]){
case 'discordbot ':
try {
const discordbot = new Discord.MessageEmbed()
.setTitle('My Discord Bot*')
.addFields(
{ name: 'Test Bot:', value: 'Test' },
message.channel.send(discordbot );
} catch (err) {
catchErr(err, message);
}
.send returns a Promise, if you want to catch errors you can either go for Promise.catch or use async / await (this requires your top level function to be async)
// Using Promise.catch
const catchErr = err => {
console.log(err)
}
message.channel.send(embed).catch(catchErr)
// Using async / await
try {
await message.channel.send(embed)
} catch(err) {
catchErr(err)
}
function sendMessage(message,content){
const permissions = message.channel.permissionsFor(message.client.user);
if (permissions.has("SEND_MESSAGES")){
if(content){
message.channel.send(content);
}
}
else{
console.log("[ERR] no msg perms")
message.author.send("I can't send messages on this channel or guild!, Try another channel or contact the server staff to check bot's permissions.");
}
}
Trying to make this work:
try{
message.author.send(helpEmbed)
}
catch(error) {
message.channel.send("Your dms are closed, cannot send help message.")
return;
}
message.react('✅')
But catch(error) doesn't work for a promise error (as the actual error log calls it UnhandledPromiseRejectionWarning). And I can't use .catch(() => etc because then I can't put a return inside of it, meaning it won't stop the message.react. And in a try/catch you can't leave the catch() empty.
How Can I handle error in React-Native
So this is my error enums
export const frontendErrors = {
INVALID_PHONE_NUMBER_ENTERED: 'Sorry you have entered invalid phone number'
}
From my phoneAuth.js file, I am throwing error in catch statement like this
catch (err) {
const errorSearch = 'auth/invalid-phone-number'
if (err.message.includes(errorSearch)) {
throw new Error(frontendErrors.INVALID_PHONE_NUMBER_ENTERED)
}
And When I catching the error in the parent component
catch (error) {
console.log(error)
this.dropDownAlertRef.alertWithType('error', 'Error', error)
}
in the log I am getting my error as an object and like this
Error: Sorry you have entered invalid phone number
The dropdownAlertRef takes in string so it is logging an empty object i.e.
this.dropDownAlertRef.alertWithType('error', 'Error', 'Invalid phone number')
this will log error.
What have I tried?
Doing, console.log(error.Error) gives undefined. How can I access my error?
You can do something like this.
console.log(error.message)
For more refrence you can refer here