Skip to content

Commit

Permalink
feat(middleware): add UnhandledError middleware to handle unhandled e…
Browse files Browse the repository at this point in the history
…rrors
  • Loading branch information
dsevillamartin committed Apr 15, 2017
1 parent 93d1900 commit abca414
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
16 changes: 14 additions & 2 deletions lib/Discord/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,23 @@ class Client extends DiscordClient {
let middleware = this.middleware.array().sort((a, b) => b.priority - a.priority);
let i = 0;

const handleErr = (err, currentMiddleware) =>
middleware[middleware.length - 1].run(msg, args, next, currentMiddleware, err);

const next = (err) => {
let currentMiddleware = middleware[i] || middleware[i - 1];
let nextMiddleware = middleware[i++];
if (err) return msg.channel.sendMessage([`❌ An unexpected error occurred when trying to run middleware \`${currentMiddleware.constructor.name}\``, `\`${err}\``]);
if (nextMiddleware) nextMiddleware.run(msg, args, next, command);
if (err) return handleErr(err, currentMiddleware);
if (nextMiddleware) {
try {
const thisMiddleware = nextMiddleware.run(msg, args, next, command);
if (thisMiddleware.catch && typeof thisMiddleware.catch === 'function') {
thisMiddleware.catch(e => handleErr(e, nextMiddleware || currentMiddleware));
}
} catch (e) {
handleErr(err, nextMiddleware || currentMiddleware);
}
}
};

next();
Expand Down
20 changes: 9 additions & 11 deletions lib/Discord/Modules/CasualHelp.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class CasualHelpModule extends Module {
this.cooldowns = new Discord.Collection();
}

get priority() {
return 1;
}

get help() {
return {
general: this.textToEmbed('Yappy, the GitLab Monitor - Information', [
Expand Down Expand Up @@ -43,11 +47,13 @@ class CasualHelpModule extends Module {
}

run(msg, args, next) {
if (msg.content.split(' ').length < 2) return;
if (msg.content.split(' ').length < 3) return next();
if (!msg.content.includes('yappy') || !msg.content.includes('gitlab')) return next();
let text = msg.content;

this._request(msg, text)
return this._request(msg, text)
.then((res) => {
if (!res.body.result) return next(res.body.status ? res.body.status.errorDetails : res.body);
const result = res.body.result;
const action = result.action;
const helpMessages = this.help;
Expand All @@ -61,21 +67,13 @@ class CasualHelpModule extends Module {
if (action === 'help.general') msg.channel.sendEmbed(helpMessages.general);
if (action === 'help.webhook') msg.channel.sendEmbed(helpMessages.webhook);
this.cooldowns.set(msg.channel.id, Date.now());
})
.catch((res) => {
Log.error(res.body);
// Log.parseWebError(res.body);
});
}

_request(msg, text) {
const url = `https://api.api.ai/api/query?v=20150910&v=20150910&query=${encodeURI(text)}&lang=en&sessionId=${msg.id}`;
return snekfetch.get(url)
.set('Authorization', 'Bearer 79eb39589b364c74b2504b322390139a')
.catch((res) => {
Log.error(res.body);
this.moduleError(msg, res.body.status ? res.body.status.errorDetails : res.body);
});
.set('Authorization', 'Bearer 79eb39589b364c74b2504b322390139a');
}

calculateCooldownLeft(msg) {
Expand Down
13 changes: 13 additions & 0 deletions lib/Discord/Modules/UnhandledError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Module = require('../Module');

class UnhandledErrorModule extends Module {
run(msg, args, next, middleware, error) {
if (!error) return;
let embed = this.textToEmbed(`Yappy, the GitLab Monitor - Unhandled Error: \`${middleware ? middleware.constructor.name : msg.cleanContent}\``, '', '#CE0814');
if (typeof error === 'string') embed.setDescription(error);

return msg.channel.sendEmbed(embed);
}
}

module.exports = UnhandledErrorModule;

0 comments on commit abca414

Please sign in to comment.