feat: improved error messages

This commit is contained in:
TrèsAbhi committed 2024-01-19 16:07:15 -06:00
1 parent 890a4d142b
commit 4c746cbb86
6 files changed
+32 -25

No files matched your search

+2 -5
View File
@@ -337,11 +337,8 @@ export const ratingsCommand = new Promise<CommandRegistryRaw>(
if (playerIndex === -1) { if (playerIndex === -1) {
throw new UserError( throw new UserError(
`${accountInfo.nickname} didn't player ratings in season ${season}`, `${accountInfo.nickname} didn't play ratings in season ${season}`,
{ 'This player did not participate in this season or did not get past calibration.',
cause:
'This player did not participate in this season or did not get past calibration.',
},
); );
} }
+4 -4
View File
@@ -10,10 +10,10 @@ export default async function getTankStats(region: Region, id: number) {
); );
if (tankStats[id] === null) { if (tankStats[id] === null) {
throw new UserError('No tank stats available', { throw new UserError(
cause: 'No tank stats available',
"This player doesn't have any stats for tanks available. This may not be the player you are looking for.", "This player doesn't have any stats available. This may not be the player you are looking for.",
}); );
} }
return tankStats[id]; return tankStats[id];
+8 -6
View File
@@ -15,9 +15,10 @@ export default async function resolveTankId(tank: string | number) {
}); });
if (searchResult.length === 0) { if (searchResult.length === 0) {
throw new UserError('Tank not found', { throw new UserError(
cause: `Could not find tank by the name "${tank}".`, 'Tank not found',
}); `Could not find tank by the name "${tank}".`,
);
} else { } else {
return searchResult[0].obj.id; return searchResult[0].obj.id;
} }
@@ -25,9 +26,10 @@ export default async function resolveTankId(tank: string | number) {
if ((await tankDefinitions)[number]) { if ((await tankDefinitions)[number]) {
return number; return number;
} else { } else {
throw new UserError('Tank not found', { throw new UserError(
cause: `Could not find tank by the ID "${number}".`, 'Tank not found',
}); `Could not find tank by the ID "${number}". Try typing the name of the tank instead of a number.`,
);
} }
} }
} }
+6 -1
View File
@@ -1,4 +1,9 @@
/** /**
* Discriminator for user caused errors to not report in logs * 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,
) {}
}
+4 -3
View File
@@ -18,9 +18,10 @@ export default async function resolveClanFromCommand(
if (accounts[0]) { if (accounts[0]) {
return { region: accounts[0].region, id: accounts[0].clan_id }; return { region: accounts[0].region, id: accounts[0].clan_id };
} else { } else {
throw new UserError('Could not find clan', { throw new UserError(
cause: `I couldn't find clan \`${clan}\`. Try selecting a username from the search result.`, 'Could not find clan',
}); `\`${clan}\` was not found. Try selecting a clan from the search result.`,
);
} }
} }
} }
+8 -6
View File
@@ -34,11 +34,12 @@ export default async function resolvePlayerFromCommand(
id: accounts[0].account_id, id: accounts[0].account_id,
} satisfies ResolvedPlayer; } satisfies ResolvedPlayer;
} else { } else {
throw new UserError('Could not find user', { throw new UserError(
cause: `I couldn't find user "${markdownEscape( 'Could not find user',
`I couldn't find user "${markdownEscape(
commandUsername, commandUsername,
)}". Try picking an user from the search result, typing in a valid username, or using the \`/link\` command.`, )}". Try picking an user from the search result, typing in a valid username, or using the \`/link\` command.`,
}); );
} }
} }
} else { } else {
@@ -47,9 +48,10 @@ export default async function resolvePlayerFromCommand(
if (account) { if (account) {
return { region: account.region, id: account.blitz }; return { region: account.region, id: account.blitz };
} else { } else {
throw new UserError('Use the `/link` command', { throw new UserError(
cause: 'Link your Blitz and Discord accounts to get started.', "You're account isn't linked with Blitzkrieg yet",
}); 'Use the `/link` command to get started.', // TODO: add shortcut button too
);
} }
} }
} }