From 4c746cbb864bd6967ea634aac7f4fb27658c426f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=C3=A8sAbhi?= <43380238+tresabhi@users.noreply.github.com> Date: Fri, 19 Jan 2024 16:07:15 -0600 Subject: [PATCH] feat: improved error messages --- src/commands/ratings.tsx | 7 ++----- src/core/blitz/getTankStats.ts | 8 ++++---- src/core/blitz/resolveTankId.ts | 14 ++++++++------ src/core/blitzkrieg/userError.ts | 7 ++++++- src/core/discord/resolveClanFromCommand.ts | 7 ++++--- src/core/discord/resolvePlayerFromCommand.ts | 14 ++++++++------ 6 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/commands/ratings.tsx b/src/commands/ratings.tsx index 68ab00559..e238e2063 100644 --- a/src/commands/ratings.tsx +++ b/src/commands/ratings.tsx @@ -337,11 +337,8 @@ export const ratingsCommand = new Promise( if (playerIndex === -1) { throw new UserError( - `${accountInfo.nickname} didn't player ratings in season ${season}`, - { - cause: - 'This player did not participate in this season or did not get past calibration.', - }, + `${accountInfo.nickname} didn't play ratings in season ${season}`, + 'This player did not participate in this season or did not get past calibration.', ); } diff --git a/src/core/blitz/getTankStats.ts b/src/core/blitz/getTankStats.ts index 4965c43d7..763d720cf 100644 --- a/src/core/blitz/getTankStats.ts +++ b/src/core/blitz/getTankStats.ts @@ -10,10 +10,10 @@ export default async function getTankStats(region: Region, id: number) { ); if (tankStats[id] === null) { - throw new UserError('No tank stats available', { - cause: - "This player doesn't have any stats for tanks available. This may not be the player you are looking for.", - }); + throw new UserError( + 'No tank stats available', + "This player doesn't have any stats available. This may not be the player you are looking for.", + ); } return tankStats[id]; diff --git a/src/core/blitz/resolveTankId.ts b/src/core/blitz/resolveTankId.ts index c110ee3a2..813f5e51a 100644 --- a/src/core/blitz/resolveTankId.ts +++ b/src/core/blitz/resolveTankId.ts @@ -15,9 +15,10 @@ export default async function resolveTankId(tank: string | number) { }); if (searchResult.length === 0) { - throw new UserError('Tank not found', { - cause: `Could not find tank by the name "${tank}".`, - }); + throw new UserError( + 'Tank not found', + `Could not find tank by the name "${tank}".`, + ); } else { return searchResult[0].obj.id; } @@ -25,9 +26,10 @@ export default async function resolveTankId(tank: string | number) { if ((await tankDefinitions)[number]) { return number; } else { - throw new UserError('Tank not found', { - cause: `Could not find tank by the ID "${number}".`, - }); + throw new UserError( + 'Tank not found', + `Could not find tank by the ID "${number}". Try typing the name of the tank instead of a number.`, + ); } } } diff --git a/src/core/blitzkrieg/userError.ts b/src/core/blitzkrieg/userError.ts index 06b0ee65c..e4eb7cc27 100644 --- a/src/core/blitzkrieg/userError.ts +++ b/src/core/blitzkrieg/userError.ts @@ -1,4 +1,9 @@ /** * Discriminator for user caused errors to not report in logs */ -export class UserError extends Error {} +export class UserError { + constructor( + public message: string, + public cause: string, + ) {} +} diff --git a/src/core/discord/resolveClanFromCommand.ts b/src/core/discord/resolveClanFromCommand.ts index a43bb78fc..f332ca22e 100644 --- a/src/core/discord/resolveClanFromCommand.ts +++ b/src/core/discord/resolveClanFromCommand.ts @@ -18,9 +18,10 @@ export default async function resolveClanFromCommand( if (accounts[0]) { return { region: accounts[0].region, id: accounts[0].clan_id }; } else { - throw new UserError('Could not find clan', { - cause: `I couldn't find clan \`${clan}\`. Try selecting a username from the search result.`, - }); + throw new UserError( + 'Could not find clan', + `\`${clan}\` was not found. Try selecting a clan from the search result.`, + ); } } } diff --git a/src/core/discord/resolvePlayerFromCommand.ts b/src/core/discord/resolvePlayerFromCommand.ts index a766f2161..21ca22347 100644 --- a/src/core/discord/resolvePlayerFromCommand.ts +++ b/src/core/discord/resolvePlayerFromCommand.ts @@ -34,11 +34,12 @@ export default async function resolvePlayerFromCommand( id: accounts[0].account_id, } satisfies ResolvedPlayer; } else { - throw new UserError('Could not find user', { - cause: `I couldn't find user "${markdownEscape( + throw new UserError( + 'Could not find user', + `I couldn't find user "${markdownEscape( commandUsername, )}". Try picking an user from the search result, typing in a valid username, or using the \`/link\` command.`, - }); + ); } } } else { @@ -47,9 +48,10 @@ export default async function resolvePlayerFromCommand( if (account) { return { region: account.region, id: account.blitz }; } else { - throw new UserError('Use the `/link` command', { - cause: 'Link your Blitz and Discord accounts to get started.', - }); + throw new UserError( + "You're account isn't linked with Blitzkrieg yet", + 'Use the `/link` command to get started.', // TODO: add shortcut button too + ); } } }