Skip to content

docs: improve UserError docs #722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lib/errors/UserError.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
/**
* The UserError class to be emitted in the pieces.
* @property name This will be `'UserError'` and can be used to distinguish the type of error when any error gets thrown
* @example
* ```typescript
* throw new UserError({
* identifier: 'AddArgumentError',
* message: 'You must write two numbers, but the second one did not match.',
* context: { received: 2, expected: 3 }
* });
* ```
*/
export class UserError extends Error {
/**
Expand Down
32 changes: 24 additions & 8 deletions src/lib/parsers/Args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ export class Args {
* });
*
* const a = await args.pickResult(resolver);
* if (!a.success) throw new UserError('ArgumentNumberNaN', 'You must write a valid number.');
* if (!a.success) {
* throw new UserError({ identifier: 'ArgumentNumberNaN', message: 'You must write a valid number.' });
* }
*
* await message.channel.send(`The result is: ${a.value ** 2}!`);
* // Sends "The result is: 25"
Expand All @@ -103,10 +105,14 @@ export class Args {
* ```typescript
* // !add 1 2
* const a = await args.pickResult('integer');
* if (!a.success) throw new UserError('AddArgumentError', 'You must write two numbers, but the first one did not match.');
* if (!a.success) {
* throw new UserError({ identifier: 'AddArgumentError', message: 'You must write two numbers, but the first one did not match.' });
* }
*
* const b = await args.pickResult('integer');
* if (!b.success) throw new UserError('AddArgumentError', 'You must write two numbers, but the second one did not match.');
* if (!b.success) {
* throw new UserError({ identifier: 'AddArgumentError', message: 'You must write two numbers, but the second one did not match.' });
* }
*
* await message.channel.send(`The result is: ${a.value + b.value}!`);
* // Sends "The result is: 3"
Expand Down Expand Up @@ -186,7 +192,9 @@ export class Args {
* const resolver = Args.make((parameter) => Args.ok(parameter.split('').reverse()));
*
* const a = await args.restResult(resolver);
* if (!a.success) throw new UserError('AddArgumentError', 'You must write some text.');
* if (!a.success) {
* throw new UserError({ identifier: 'AddArgumentError', message: 'You must write some text.' });
* }
*
* await message.channel.send(`The reversed value is... ${a.value}`);
* // Sends "The reversed value is... !dlrow olleH"
Expand All @@ -201,10 +209,14 @@ export class Args {
* ```typescript
* // !add 2 Hello World!
* const a = await args.pickResult('integer');
* if (!a.success) throw new UserError('AddArgumentError', 'You must write a number and a text, but the former did not match.');
* if (!a.success) {
* throw new UserError({ identifier: 'AddArgumentError', message: 'You must write a number and a text, but the former did not match.' });
* }
*
* const b = await args.restResult('string', { minimum: 1 });
* if (!b.success) throw new UserError('AddArgumentError', 'You must write a number and a text, but the latter did not match.');
* if (!b.success) {
* throw new UserError({ identifier: 'AddArgumentError', message: 'You must write a number and a text, but the latter did not match.' });
* }
*
* await message.channel.send(`The repeated value is... ${b.value.repeat(a.value)}!`);
* // Sends "The repeated value is... Hello World!Hello World!"
Expand Down Expand Up @@ -272,7 +284,9 @@ export class Args {
* // !add 2 Hello World!
* const resolver = Args.make((arg) => Args.ok(arg.split('').reverse()));
* const result = await args.repeatResult(resolver, { times: 5 });
* if (!result.success) throw new UserError('CountArgumentError', 'You must write up to 5 words.');
* if (!result.success) {
* throw new UserError({ identifier: 'CountArgumentError', message: 'You must write up to 5 words.' });
* }
*
* await message.channel.send(`You have written ${result.value.length} word(s): ${result.value.join(' ')}`);
* // Sends "You have written 2 word(s): olleH !dlroW"
Expand All @@ -287,7 +301,9 @@ export class Args {
* ```typescript
* // !reverse-each 2 Hello World!
* const result = await args.repeatResult('string', { times: 5 });
* if (!result.success) throw new UserError('CountArgumentError', 'You must write up to 5 words.');
* if (!result.success) {
* throw new UserError({ identifier: 'CountArgumentError', message: 'You must write up to 5 words.' });
* }
*
* await message.channel.send(`You have written ${result.value.length} word(s): ${result.value.join(' ')}`);
* // Sends "You have written 2 word(s): Hello World!"
Expand Down