mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 11:39:50 +02:00
feat: mature error handling system
This commit is contained in:
1 parent
1875f30cf5
commit
524edac615
23 files changed
+87
-87
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import negativeEmbed from '../interaction/negativeEmbed.js';
|
||||
import errorWithCause from '../process/errorWithCause.js';
|
||||
import listAccountsPanServer, {
|
||||
usernamePatternWithoutPosition,
|
||||
} from './listAccountsPanServer.js';
|
||||
@@ -17,17 +17,9 @@ export default async function getBlitzAccount(
|
||||
const username = commandUsername ?? displayName;
|
||||
|
||||
if (username === undefined) {
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
negativeEmbed(
|
||||
'Username could not be inferred',
|
||||
"You didn't provide me a username in the command nor was I able to infer your Blitz username from your Discord nickname. Try providing a username manually in the command or using the `/verify` command to set your Discord nickname to your Blitz username.",
|
||||
),
|
||||
],
|
||||
});
|
||||
|
||||
throw new Error(
|
||||
`No username provided commandUsername: ${commandUsername} displayName: ${displayName} username: ${username}`,
|
||||
throw errorWithCause(
|
||||
'Username could not be inferred',
|
||||
"You didn't provide me a username in the command nor was I able to infer your Blitz username from your Discord nickname. Try providing a username manually in the command or using the `/verify` command to set your Discord nickname to your Blitz username.",
|
||||
);
|
||||
}
|
||||
|
||||
@@ -40,16 +32,10 @@ export default async function getBlitzAccount(
|
||||
if (accounts[0]) {
|
||||
return { server: accounts[0].server, id: accounts[0].account_id };
|
||||
} else {
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
negativeEmbed(
|
||||
'Could not find user',
|
||||
`I couldn't find user \`${username}\`. Try providing a username manually or using the \`/verify\` command to set your Discord nickname to your Blitz username.`,
|
||||
),
|
||||
],
|
||||
});
|
||||
|
||||
throw new Error(`Could not find user "${username}"`);
|
||||
throw errorWithCause(
|
||||
'Could not find user',
|
||||
`I couldn't find user \`${username}\`. Try providing a valid username manually or using the \`/verify\` command to set your Discord nickname to your Blitz username.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import negativeEmbed from '../interaction/negativeEmbed.js';
|
||||
import errorWithCause from '../process/errorWithCause.js';
|
||||
import { serverAndIdPattern } from './getBlitzAccount.js';
|
||||
import listClansPanServer from './listClansPanServer.js';
|
||||
|
||||
@@ -17,16 +17,10 @@ export default async function getBlitzClan(
|
||||
if (accounts[0]) {
|
||||
return { server: accounts[0].server, id: accounts[0].clan_id };
|
||||
} else {
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
negativeEmbed(
|
||||
'Could not find clan',
|
||||
`I couldn't find clan \`${clan}\`. Try selecting a username from the search result.`,
|
||||
),
|
||||
],
|
||||
});
|
||||
|
||||
throw new Error(`Could not find clan "${clan}"`);
|
||||
throw errorWithCause(
|
||||
'Could not find clan',
|
||||
`I couldn't find clan \`${clan}\`. Try selecting a username from the search result.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
||||
import { BLITZ_SERVERS, BlitzServer } from '../../constants/servers.js';
|
||||
import { TanksStats } from '../../types/tanksStats.js';
|
||||
import negativeEmbed from '../interaction/negativeEmbed.js';
|
||||
import { args } from '../process/args.js';
|
||||
import errorWithCause from '../process/errorWithCause.js';
|
||||
import getWargamingResponse from './getWargamingResponse.js';
|
||||
|
||||
export default async function getTankStats(
|
||||
@@ -15,16 +15,10 @@ export default async function getTankStats(
|
||||
);
|
||||
|
||||
if (tankStats[id] === null) {
|
||||
interaction.editReply({
|
||||
embeds: [
|
||||
negativeEmbed(
|
||||
'No tank stats available',
|
||||
`Wargaming says there is no tank stats for this account. This account may not have any battles/tanks or exist in the ${BLITZ_SERVERS[server]} server.`,
|
||||
),
|
||||
],
|
||||
});
|
||||
|
||||
throw new Error(`No tank stats available for ${server}/${id}`);
|
||||
throw errorWithCause(
|
||||
'No tank stats available',
|
||||
`Wargaming says there is no tank stats for this account. This account may not have any battles/tanks or exist in the ${BLITZ_SERVERS[server]} server.`,
|
||||
);
|
||||
}
|
||||
|
||||
return tankStats[id];
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { WargamingResponse } from '../../types/wargamingResponse.js';
|
||||
import errorWithCause from '../process/errorWithCause.js';
|
||||
|
||||
export default async function getWargamingResponse<Data extends object>(
|
||||
url: string,
|
||||
@@ -11,8 +12,9 @@ export default async function getWargamingResponse<Data extends object>(
|
||||
if (parsed.status === 'ok') {
|
||||
return parsed.data;
|
||||
} else {
|
||||
throw new Error(
|
||||
`Wargaming response error status:"${parsed.status}" message: "${parsed.error.message}" url: "${url}"`,
|
||||
throw errorWithCause(
|
||||
`Wargaming response error status:"${parsed.status}"`,
|
||||
`Message: "${parsed.error.message}"\nURL: "${url}"`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
||||
import { go } from 'fuzzysort';
|
||||
import negativeEmbed from '../interaction/negativeEmbed.js';
|
||||
import errorWithCause from '../process/errorWithCause.js';
|
||||
import { TANKS, tankopedia } from './tankopedia.js';
|
||||
|
||||
export default async function resolveTankId(
|
||||
@@ -13,16 +13,10 @@ export default async function resolveTankId(
|
||||
const searchResult = go(tank, TANKS, { keys: ['name'], limit: 1 });
|
||||
|
||||
if (searchResult.length === 0) {
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
negativeEmbed(
|
||||
'Tank not found',
|
||||
`Could not find tank by the name "${tank}".`,
|
||||
),
|
||||
],
|
||||
});
|
||||
|
||||
throw new Error(`Tank not found by name "${tank}"`);
|
||||
throw errorWithCause(
|
||||
'Tank not found',
|
||||
`Could not find tank by the name "${tank}".`,
|
||||
);
|
||||
} else {
|
||||
return searchResult[0].obj.tank_id;
|
||||
}
|
||||
@@ -30,16 +24,10 @@ export default async function resolveTankId(
|
||||
if (tankopedia[number]) {
|
||||
return number;
|
||||
} else {
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
negativeEmbed(
|
||||
'Tank not found',
|
||||
`Could not find tank by the ID "${number}".`,
|
||||
),
|
||||
],
|
||||
});
|
||||
|
||||
throw new Error(`Tank not found by ID "${number}"`);
|
||||
throw errorWithCause(
|
||||
'Tank not found',
|
||||
`Could not find tank by the ID "${number}".`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,5 +22,5 @@ export default async function wargamingError(
|
||||
});
|
||||
|
||||
console.log(`Wargaming returned an error`);
|
||||
handleError(error, client, command);
|
||||
handleError(error, command);
|
||||
}
|
||||
@@ -24,5 +24,5 @@ export default async function blitzStarsError(
|
||||
});
|
||||
|
||||
console.log(`BlitzStars returned an error`);
|
||||
handleError(error, client, command);
|
||||
handleError(error, command);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { argv } from 'process';
|
||||
import errorWithCause from './errorWithCause.js';
|
||||
|
||||
type ArgumentName = 'discord-token' | 'wargaming-application-id';
|
||||
|
||||
@@ -16,6 +17,9 @@ argv.forEach((arg) => {
|
||||
|
||||
Object.keys(args).forEach((arg) => {
|
||||
if (args[arg as ArgumentName] === '') {
|
||||
throw new Error(`Missing argument: ${arg}`);
|
||||
throw errorWithCause(
|
||||
`Missing argument: ${arg}`,
|
||||
'Missing required auth key.',
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
export default function errorWithCause(message: string, cause: string) {
|
||||
return new Error(message, { cause });
|
||||
}
|
||||
Reference in new issue
Block a user