Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider 967b1385 rédigé par Will Hunt's avatar Will Hunt Validation de GitHub
Parcourir les fichiers

Merge pull request #313 from Half-Shot/soru/unhandled-promise-rejection

add try...catch everywhere
parents b335beba 2b76ec7e
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -103,24 +103,51 @@ export class DiscordBot {
const client = await this.clientFactory.getClient();
if (!this.config.bridge.disableTypingNotifications) {
client.on("typingStart", async (c, u) => this.OnTyping(c, u, true) );
client.on("typingStop", async (c, u) => this.OnTyping(c, u, false) );
client.on("typingStart", async (c, u) => {
try {
await this.OnTyping(c, u, true);
} catch (err) { log.warning("Exception thrown while handling \"typingStart\" event", err); }
});
client.on("typingStop", async (c, u) => {
try {
await this.OnTyping(c, u, false);
} catch (err) { log.warning("Exception thrown while handling \"typingStop\" event", err); }
});
}
if (!this.config.bridge.disablePresence) {
client.on("presenceUpdate", (_, newMember: Discord.GuildMember) => {
try {
this.presenceHandler.EnqueueUser(newMember.user);
} catch (err) { log.warning("Exception thrown while handling \"presenceUpdate\" event", err); }
});
}
this.channelSync = new ChannelSyncroniser(this.bridge, this.config, this);
client.on("channelUpdate", async (_, newChannel) => this.channelSync.OnUpdate(newChannel) );
client.on("channelDelete", async (channel) => this.channelSync.OnDelete(channel) );
client.on("guildUpdate", async (_, newGuild) => this.channelSync.OnGuildUpdate(newGuild) );
client.on("guildDelete", async (guild) => this.channelSync.OnGuildDelete(guild) );
client.on("channelUpdate", async (_, newChannel) => {
try {
await this.channelSync.OnUpdate(newChannel);
} catch (err) { log.error("Exception thrown while handling \"channelUpdate\" event", err); }
});
client.on("channelDelete", async (channel) => {
try {
await this.channelSync.OnDelete(channel);
} catch (err) { log.error("Exception thrown while handling \"channelDelete\" event", err); }
});
client.on("guildUpdate", async (_, newGuild) => {
try {
await this.channelSync.OnGuildUpdate(newGuild);
} catch (err) { log.error("Exception thrown while handling \"guildUpdate\" event", err); }
});
client.on("guildDelete", async (guild) => {
try {
await this.channelSync.OnGuildDelete(guild);
} catch (err) { log.error("Exception thrown while handling \"guildDelete\" event", err); }
});
// Due to messages often arriving before we get a response from the send call,
// messages get delayed from discord. We use Bluebird.delay to handle this.
client.on("messageDelete", async (msg: Discord.Message) => {
try {
await Bluebird.delay(this.config.limits.discordSendDelay);
this.discordMessageQueue[msg.channel.id] = (async () => {
await (this.discordMessageQueue[msg.channel.id] || Promise.resolve());
......@@ -130,8 +157,12 @@ export class DiscordBot {
log.error("Caught while handing 'messageDelete'", err);
}
})();
} catch (err) {
log.error("Exception thrown while handling \"messageDelete\" event", err);
}
});
client.on("messageUpdate", async (oldMessage: Discord.Message, newMessage: Discord.Message) => {
try {
await Bluebird.delay(this.config.limits.discordSendDelay);
this.discordMessageQueue[newMessage.channel.id] = (async () => {
await (this.discordMessageQueue[newMessage.channel.id] || Promise.resolve());
......@@ -141,8 +172,12 @@ export class DiscordBot {
log.error("Caught while handing 'messageUpdate'", err);
}
})();
} catch (err) {
log.error("Exception thrown while handling \"messageUpdate\" event", err);
}
});
client.on("message", async (msg: Discord.Message) => {
try {
await Bluebird.delay(this.config.limits.discordSendDelay);
this.discordMessageQueue[msg.channel.id] = (async () => {
await (this.discordMessageQueue[msg.channel.id] || Promise.resolve());
......@@ -152,15 +187,33 @@ export class DiscordBot {
log.error("Caught while handing 'message'", err);
}
})();
} catch (err) {
log.error("Exception thrown while handling \"message\" event", err);
}
});
const jsLog = new Log("discord.js");
this.userSync = new UserSyncroniser(this.bridge, this.config, this);
client.on("userUpdate", async (_, user) => this.userSync.OnUpdateUser(user));
client.on("guildMemberAdd", async (user) => this.userSync.OnAddGuildMember(user));
client.on("guildMemberRemove", async (user) => this.userSync.OnRemoveGuildMember(user));
client.on("guildMemberUpdate", async (oldUser, newUser) =>
this.userSync.OnUpdateGuildMember(oldUser, newUser));
client.on("userUpdate", async (_, user) => {
try {
await this.userSync.OnUpdateUser(user);
} catch (err) { log.error("Exception thrown while handling \"userUpdate\" event", err); }
});
client.on("guildMemberAdd", async (user) => {
try {
await this.userSync.OnAddGuildMember(user);
} catch (err) { log.error("Exception thrown while handling \"guildMemberAdd\" event", err); }
});
client.on("guildMemberRemove", async (user) => {
try {
await this.userSync.OnRemoveGuildMember(user);
} catch (err) { log.error("Exception thrown while handling \"guildMemberRemove\" event", err); }
});
client.on("guildMemberUpdate", async (oldUser, newUser) => {
try {
await this.userSync.OnUpdateGuildMember(oldUser, newUser);
} catch (err) { log.error("Exception thrown while handling \"guildMemberUpdate\" event", err); }
});
client.on("debug", (msg) => { jsLog.verbose(msg); });
client.on("error", (msg) => { jsLog.error(msg); });
client.on("warn", (msg) => { jsLog.warn(msg); });
......
......@@ -67,10 +67,23 @@ async function run(port: number, fileConfig: DiscordBridgeConfig) {
clientFactory,
controller: {
// onUserQuery: userQuery,
onAliasQueried: roomhandler.OnAliasQueried.bind(roomhandler),
onAliasQuery: roomhandler.OnAliasQuery.bind(roomhandler),
onEvent: (request, context) =>
request.outcomeFrom(Bluebird.resolve(roomhandler.OnEvent(request, context))),
onAliasQueried: async (alias: string, roomId: string) => {
try {
await roomhandler.OnAliasQueried.bind(roomhandler);
} catch (err) { log.error("Exception thrown while handling \"onAliasQueried\" event", err); }
},
onAliasQuery: async (alias: string, aliasLocalpart: string) => {
try {
await roomhandler.OnAliasQuery.bind(roomhandler);
} catch (err) { log.error("Exception thrown while handling \"onAliasQuery\" event", err); }
},
onEvent: async (request, context) => {
try {
await request.outcomeFrom(Bluebird.resolve(roomhandler.OnEvent(request, context)));
} catch (err) {
log.error("Exception thrown while handling \"onEvent\" event", err);
}
},
onLog: (line, isError) => {
log.verbose("matrix-appservice-bridge", line);
},
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment