Better Error Handling

This commit is contained in:
Daniel Odendahl Jr
2017-05-05 21:37:30 +00:00
parent d8a773958d
commit 13cbd23f2f
54 changed files with 120 additions and 156 deletions
+1 -1
View File
@@ -42,7 +42,7 @@ module.exports = class BotSearchCommand extends Command {
body.prefix, true);
return msg.embed(embed);
} catch(err) {
return msg.say('An Error Occurred. The bot may not have been found.');
return msg.say(`An Error Occurred: ${err}`);
}
}
};
+2 -1
View File
@@ -28,13 +28,14 @@ module.exports = class DefineCommand extends Command {
try {
const { body } = await request
.get(`http://api.wordnik.com:80/v4/word.json/${query}/definitions?limit=1&includeRelated=false&useCanonical=false&api_key=${process.env.WORDNIK_KEY}`);
if(body.length === 0) throw new Error('No Results.');
const embed = new RichEmbed()
.setColor(0x9797FF)
.setTitle(body[0].word)
.setDescription(body[0].text);
return msg.embed(embed);
} catch(err) {
return msg.say('An Error Occurred. The word may not have been found.');
return msg.say(`An Error Occurred: ${err}`);
}
}
};
+1 -2
View File
@@ -18,8 +18,7 @@ module.exports = class DiscrimCommand extends Command {
prompt: 'Which discriminator would you like to search for?',
type: 'string',
validate: discrim => {
if(/[0-9]+$/g.test(discrim) && discrim.length === 4)
return true;
if(/[0-9]+$/g.test(discrim) && discrim.length === 4) return true;
return `${discrim} is not a valid discriminator.`;
}
}
+2 -1
View File
@@ -27,6 +27,7 @@ module.exports = class ForecastCommand extends Command {
try {
const { body } = await request
.get(`https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where u=\'f\' AND woeid in (select woeid from geo.places(1) where text="${query}")&format=json`);
if(body.query.count === 0) throw new Error('Location Not Found.');
const forecasts = body.query.results.channel.item.forecast;
const embed = new RichEmbed()
.setColor(0x0000FF)
@@ -49,7 +50,7 @@ module.exports = class ForecastCommand extends Command {
`**High:** ${forecasts[6].high}°F, **Low:** ${forecasts[6].low}°F, **Condition:** ${forecasts[6].text}`);
return msg.embed(embed);
} catch(err) {
return msg.say('An Error Occurred. The location may not have been found.');
return msg.say(`An Error Occurred: ${err}`);
}
}
};
+2 -2
View File
@@ -29,11 +29,11 @@ module.exports = class GoogleCommand extends Command {
.get(`https://www.google.com/search?q=${query}`);
const $ = cheerio.load(text);
let href = $('.r').first().find('a').first().attr('href');
if(!href) throw new Error('No Results');
if(!href) throw new Error('No Results.');
href = querystring.parse(href.replace('/url?', ''));
return message.edit(href.q);
} catch(err) {
return message.edit('No Results Found.');
return message.edit('An Error Occurred: No Results.');
}
}
};
+2 -1
View File
@@ -28,6 +28,7 @@ module.exports = class IMDBCommand extends Command {
try {
const { body } = await request
.get(`http://www.omdbapi.com/?t=${query}&plot=full`);
if(body.Error) throw new Error('No Results.');
const embed = new RichEmbed()
.setColor(0xDBA628)
.setAuthor('IMDB', 'https://i.imgur.com/sXwwIQs.png')
@@ -50,7 +51,7 @@ module.exports = class IMDBCommand extends Command {
body.Actors);
return msg.embed(embed);
} catch(err) {
return msg.say('An Error Occurred. The film may not have been found.');
return msg.say(`An Error Occurred: ${err}`);
}
}
};
+1 -1
View File
@@ -41,7 +41,7 @@ module.exports = class MapCommand extends Command {
return msg.channel.send({ files: [{ attachment: body, name: 'map.png' }] })
.catch(err => msg.say(`An Error Occurred: ${err}`));
} catch(err) {
return msg.say('An Error Occurred. The location may not have been found.');
return msg.say(`An Error Occurred: ${err}`);
}
}
};
+2 -3
View File
@@ -27,11 +27,10 @@ module.exports = class NeopetCommand extends Command {
.get(`http://www.sunnyneo.com/petimagefinder.php?name=${query}&size=5&mood=1`);
const $ = cheerio.load(text);
const link = $('textarea').first().text();
if(!link.includes('cp'))
return msg.say('This is not a valid pet name.');
if(!link.includes('cp')) throw new Error('Invalid Pet Name.');
return msg.say(link);
} catch(err) {
return msg.say('An Unknown Error Occurred.');
return msg.say(`An Error Occurred: ${err}`);
}
}
};
+2 -1
View File
@@ -28,6 +28,7 @@ module.exports = class OsuCommand extends Command {
try {
const { body } = await request
.get(`https://osu.ppy.sh/api/get_user?k=${process.env.OSU_KEY}&u=${query}&type=string`);
if(body.length === 0) throw new Error('No Results.');
const embed = new RichEmbed()
.setColor(0xFF66AA)
.setAuthor('osu!', 'https://i.imgur.com/EmnUp00.png')
@@ -58,7 +59,7 @@ module.exports = class OsuCommand extends Command {
body[0].count_rank_a, true);
return msg.embed(embed);
} catch(err) {
return msg.say('An Error Occurred. The user may not have been found.');
return msg.say(`An Error Occurred: ${err}`);
}
}
};
+2 -1
View File
@@ -28,6 +28,7 @@ module.exports = class SoundCloudCommand extends Command {
try {
const { body } = await request
.get(`https://api.soundcloud.com/tracks?q=${query}&client_id=${process.env.SOUNDCLOUD_KEY}`);
if(body.length === 0) throw new Error('No Results.');
const embed = new RichEmbed()
.setColor(0xF15A22)
.setAuthor(body[0].title, 'https://i.imgur.com/lFIz7RU.png')
@@ -45,7 +46,7 @@ module.exports = class SoundCloudCommand extends Command {
body[0].favoritings_count, true);
return msg.embed(embed);
} catch(err) {
return msg.say('An Error Occurred. The song may not have been found.');
return msg.say(`An Error Occurred: ${err}`);
}
}
};
+2 -1
View File
@@ -28,6 +28,7 @@ module.exports = class UrbanCommand extends Command {
try {
const { body } = await request
.get(`http://api.urbandictionary.com/v0/define?term=${query}`);
if(body.list.length === 0) throw new Error('No Results.');
const embed = new RichEmbed()
.setColor(0x32a8f0)
.setAuthor('Urban Dictionary', 'https://i.imgur.com/fzFuuL7.png')
@@ -38,7 +39,7 @@ module.exports = class UrbanCommand extends Command {
body.list[0].example.substr(0, 2000) || 'None');
return msg.embed(embed);
} catch(err) {
return msg.say('An Error Occurred. The word may not have been found.');
return msg.say(`An Error Occurred: ${err}`);
}
}
};
+2 -1
View File
@@ -29,6 +29,7 @@ module.exports = class WattpadCommand extends Command {
const { body } = await request
.get(`https://api.wattpad.com:443/v4/stories?query=${query}&limit=1`)
.set({ 'Authorization': `Basic ${process.env.WATTPAD_KEY}` });
if(body.stories.length === 0) throw new Error('No Results.');
const embed = new RichEmbed()
.setColor(0xF89C34)
.setAuthor('Wattpad', 'https://i.imgur.com/Rw9vRQB.png')
@@ -49,7 +50,7 @@ module.exports = class WattpadCommand extends Command {
body.stories[0].commentCount, true);
return msg.embed(embed);
} catch(err) {
return msg.say('An Error Occurred. The book may not have been found.');
return msg.say(`An Error Occurred: ${err}`);
}
}
};
+2 -1
View File
@@ -27,6 +27,7 @@ module.exports = class WeatherCommand extends Command {
try {
const { body } = await request
.get(`https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where u=\'f\' AND woeid in (select woeid from geo.places(1) where text="${query}")&format=json`);
if(body.query.count === 0) throw new Error('Location Not Found.');
const embed = new RichEmbed()
.setColor(0x0000FF)
.setAuthor(body.query.results.channel.title, 'https://i.imgur.com/2MT0ViC.png')
@@ -58,7 +59,7 @@ module.exports = class WeatherCommand extends Command {
body.query.results.channel.wind.speed, true);
return msg.embed(embed);
} catch(err) {
return msg.say('An Error Occurred. The location may not have been found.');
return msg.say(`An Error Occurred: ${err}`);
}
}
};
+2 -1
View File
@@ -28,6 +28,7 @@ module.exports = class WikipediaCommand extends Command {
try {
const { body } = await request
.get(`https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&titles=${query}&exintro=&explaintext=&redirects=&formatversion=2`);
if(body.query.pages[0].missing) throw new Error('No Results.');
const embed = new RichEmbed()
.setColor(0xE7E7E7)
.setTitle(body.query.pages[0].title)
@@ -36,7 +37,7 @@ module.exports = class WikipediaCommand extends Command {
.setDescription(body.query.pages[0].extract.substr(0, 2000).replace(/[\n]/g, '\n\n'));
return msg.embed(embed);
} catch(err) {
return msg.say('An Error Occurred. The page may not have been found.');
return msg.say(`An Error Occurred: ${err}`);
}
}
};
+2 -1
View File
@@ -28,6 +28,7 @@ module.exports = class YouTubeCommand extends Command {
try {
const { body } = await request
.get(`https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&maxResults=1&q=${query}&key=${process.env.GOOGLE_KEY}`);
if(body.items.length === 0) throw new Error('No Results.');
const embed = new RichEmbed()
.setColor(0xDD2825)
.setTitle(body.items[0].snippet.title)
@@ -37,7 +38,7 @@ module.exports = class YouTubeCommand extends Command {
.setThumbnail(body.items[0].snippet.thumbnails.default.url);
return msg.embed(embed);
} catch(err) {
return msg.say('An Error Occurred. The video may not have been found.');
return msg.say(`An Error Occurred: ${err}`);
}
}
};
+3 -2
View File
@@ -28,6 +28,7 @@ module.exports = class YuGiOhCommand extends Command {
try {
const { body } = await request
.get(`http://yugiohprices.com/api/card_data/${query}`);
if(body.status === 'fail') throw new Error('No Results.');
if(body.data.card_type === 'monster') {
const embed = new RichEmbed()
.setColor(0xBE5F1F)
@@ -35,7 +36,7 @@ module.exports = class YuGiOhCommand extends Command {
.setDescription(body.data.text)
.setAuthor('Yu-Gi-Oh!', 'https://i.imgur.com/7gPm9Rr.png')
.addField('**Card Type:**',
'Monster', true)
'monster', true)
.addField('**Type:**',
body.data.type, true)
.addField('**Attribute:**',
@@ -57,7 +58,7 @@ module.exports = class YuGiOhCommand extends Command {
body.data.card_type, true);
return msg.embed(embed);
} catch(err) {
return msg.say('An Error Occurred. The card may not have been found.');
return msg.say(`An Error Occurred: ${err}`);
}
}
};