Skip to content

Commit

Permalink
feat: improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
eartharoid committed Jun 25, 2023
1 parent 47fc7bd commit e8b95a2
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 23 deletions.
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -52,7 +52,7 @@
"@fastify/cookie": "^6.0.0",
"@fastify/jwt": "^5.0.1",
"@fastify/oauth2": "^5.1.0",
"@prisma/client": "^4.14.1",
"@prisma/client": "^4.16.1",
"boxen": "^7.1.0",
"cryptr": "^6.2.0",
"discord.js": "^14.11.0",
Expand All @@ -68,8 +68,9 @@
"node-dir": "^0.1.17",
"node-emoji": "^1.11.0",
"object-diffy": "^1.0.4",
"prisma": "^4.14.1",
"prisma": "^4.16.1",
"semver": "^7.5.1",
"short-unique-id": "^4.4.4",
"spacetime": "^7.4.4",
"terminal-link": "^2.1.1",
"yaml": "^1.10.2"
Expand Down
38 changes: 23 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/buttons/edit.js
Expand Up @@ -108,4 +108,4 @@ module.exports = class EditButton extends Button {
);
}
}
};
};
6 changes: 6 additions & 0 deletions src/lib/logging.js
Expand Up @@ -3,6 +3,11 @@ const {
EmbedBuilder,
} = require('discord.js');
const { diff: getDiff } = require('object-diffy');
const ShortUniqueId = require('short-unique-id');

const uid = new ShortUniqueId();

const getSUID = () => uid.stamp(10);

const uuidRegex = /[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}/g;

Expand Down Expand Up @@ -235,6 +240,7 @@ async function logMessageEvent(client, {

module.exports = {
getLogChannel,
getSUID,
logAdminEvent,
logMessageEvent,
logTicketEvent,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/tickets/manager.js
Expand Up @@ -28,6 +28,7 @@ const {
decrypt,
encrypt,
} = new Cryptr(process.env.ENCRYPTION_KEY);
const { getSUID } = require('../logging');

/**
* @typedef {import('@prisma/client').Category &
Expand Down Expand Up @@ -682,7 +683,7 @@ module.exports = class TicketManager {
userId: interaction.user.id,
});
} catch (error) {
const ref = require('crypto').randomUUID();
const ref = getSUID();
this.client.log.warn.tickets('An error occurred whilst creating ticket', channel.id);
this.client.log.error.tickets(ref);
this.client.log.error.tickets(error);
Expand Down
22 changes: 22 additions & 0 deletions src/listeners/autocomplete/error.js
@@ -0,0 +1,22 @@
const { Listener } = require('@eartharoid/dbf');

module.exports = class extends Listener {
constructor(client, options) {
super(client, {
...options,
emitter: client.autocomplete,
event: 'error',
});
}

async run({
completer,
error,
interaction,
}) {
this.client.log.error.autocomplete(`"${completer.id}" autocomplete execution error:`, {
error,
interaction,
});
}
};
5 changes: 3 additions & 2 deletions src/listeners/buttons/error.js
Expand Up @@ -3,6 +3,7 @@ const {
EmbedBuilder,
codeBlock,
} = require('discord.js');
const { getSUID } = require('../../lib/logging');

module.exports = class extends Listener {
constructor(client, options) {
Expand All @@ -18,7 +19,7 @@ module.exports = class extends Listener {
error,
interaction,
}) {
const ref = require('crypto').randomUUID();
const ref = getSUID();
this.client.log.error.buttons(ref);
this.client.log.error.buttons(`"${button.id}" button execution error:`, error);
let locale = null;
Expand All @@ -39,7 +40,7 @@ module.exports = class extends Listener {
.addFields([
{
name: getMessage('misc.error.fields.identifier'),
value: codeBlock(ref),
value: codeBlock(' ' + ref + ' '),
},
]),
],
Expand Down
5 changes: 3 additions & 2 deletions src/listeners/commands/error.js
Expand Up @@ -3,6 +3,7 @@ const {
EmbedBuilder,
codeBlock,
} = require('discord.js');
const { getSUID } = require('../../lib/logging');

module.exports = class extends Listener {
constructor(client, options) {
Expand All @@ -18,7 +19,7 @@ module.exports = class extends Listener {
error,
interaction,
}) {
const ref = require('crypto').randomUUID();
const ref = getSUID();
this.client.log.error.commands(ref);
this.client.log.error.commands(`"${command.name}" command execution error:`, error);
let locale = null;
Expand All @@ -39,7 +40,7 @@ module.exports = class extends Listener {
.addFields([
{
name: getMessage('misc.error.fields.identifier'),
value: codeBlock(ref),
value: codeBlock(' ' + ref + ' '),
},
]),
],
Expand Down
51 changes: 51 additions & 0 deletions src/listeners/menus/error.js
@@ -0,0 +1,51 @@
const { Listener } = require('@eartharoid/dbf');
const {
EmbedBuilder,
codeBlock,
} = require('discord.js');
const { getSUID } = require('../../lib/logging');

module.exports = class extends Listener {
constructor(client, options) {
super(client, {
...options,
emitter: client.menus,
event: 'error',
});
}

async run({
menu,
error,
interaction,
}) {
const ref = getSUID();
this.client.log.error.menus(ref);
this.client.log.error.menus(`"${menu.id}" menu execution error:`, error);
let locale = null;
if (interaction.guild) {
locale = (await this.client.prisma.guild.findUnique({
select: { locale: true },
where: { id: interaction.guild.id },
})).locale;
}
const getMessage = this.client.i18n.getLocale(locale);
const data = {
components: [],
embeds: [
new EmbedBuilder()
.setColor('Orange')
.setTitle(getMessage('misc.error.title'))
.setDescription(getMessage('misc.error.description'))
.addFields([
{
name: getMessage('misc.error.fields.identifier'),
value: codeBlock(' ' + ref + ' '),
},
]),
],
};

interaction.reply(data).catch(() => interaction.editReply(data));
}
};
51 changes: 51 additions & 0 deletions src/listeners/modals/error.js
@@ -0,0 +1,51 @@
const { Listener } = require('@eartharoid/dbf');
const {
EmbedBuilder,
codeBlock,
} = require('discord.js');
const { getSUID } = require('../../lib/logging');

module.exports = class extends Listener {
constructor(client, options) {
super(client, {
...options,
emitter: client.modals,
event: 'error',
});
}

async run({
modal,
error,
interaction,
}) {
const ref = getSUID();
this.client.log.error.modals(ref);
this.client.log.error.modals(`"${modal.id}" modal execution error:`, error);
let locale = null;
if (interaction.guild) {
locale = (await this.client.prisma.guild.findUnique({
select: { locale: true },
where: { id: interaction.guild.id },
})).locale;
}
const getMessage = this.client.i18n.getLocale(locale);
const data = {
components: [],
embeds: [
new EmbedBuilder()
.setColor('Orange')
.setTitle(getMessage('misc.error.title'))
.setDescription(getMessage('misc.error.description'))
.addFields([
{
name: getMessage('misc.error.fields.identifier'),
value: codeBlock(' ' + ref + ' '),
},
]),
],
};

interaction.reply(data).catch(() => interaction.editReply(data));
}
};

0 comments on commit e8b95a2

Please sign in to comment.