Skip to content

Commit

Permalink
fix(Args): ensure proper error types are always thrown (#601)
Browse files Browse the repository at this point in the history
This changes the `result.unwrap()` calls to `result.unwrapRaw()` to
ensure that the erorr that gets thrown for the following methods:
- `args.pick(...)`
- `args.rest(...)`
- `args.repeat(...)`
- `args.peek(...)`
This makes it so that the error that ends up in `messageCommandError`
listener will have the proper error type. Furthermore, it can also
be handled properly when chaining `.catch()` calls to the `args.X` method.

resolves #528
  • Loading branch information
favna committed Feb 5, 2023
1 parent 18ad9fa commit ee8be2e
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/lib/parsers/Args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class Args {
public async pick<K extends keyof ArgType>(type: K, options?: ArgOptions): Promise<ArgType[K]>;
public async pick<K extends keyof ArgType>(type: K, options?: ArgOptions): Promise<ArgType[K]> {
const result = await this.pickResult(type, options);
return result.unwrap();
return result.unwrapRaw();
}

/**
Expand Down Expand Up @@ -259,7 +259,7 @@ export class Args {
public async rest<K extends keyof ArgType>(type: K, options?: ArgOptions): Promise<ArgType[K]>;
public async rest<K extends keyof ArgType>(type: K, options?: ArgOptions): Promise<ArgType[K]> {
const result = await this.restResult(type, options);
return result.unwrap();
return result.unwrapRaw();
}

/**
Expand Down Expand Up @@ -299,6 +299,7 @@ export class Args {
if (this.parser.finished) return this.missingArguments();

const output: ArgType[K][] = [];

for (let i = 0, times = options.times ?? Infinity; i < times; i++) {
const result = await this.parser.singleParseAsync(async (arg) =>
argument.run(arg, {
Expand All @@ -310,6 +311,7 @@ export class Args {
...options
})
);

if (result.isErr()) {
const error = result.unwrapErr();
if (error === null) break;
Expand Down Expand Up @@ -356,7 +358,7 @@ export class Args {
public async repeat<K extends keyof ArgType>(type: K, options?: RepeatArgOptions): Promise<ArgType[K][]>;
public async repeat<K extends keyof ArgType>(type: K, options?: RepeatArgOptions): Promise<ArgType[K][]> {
const result = await this.repeatResult(type, options);
return result.unwrap();
return result.unwrapRaw();
}

/**
Expand Down Expand Up @@ -508,7 +510,7 @@ export class Args {
public async peek<K extends keyof ArgType>(type: (() => Argument.Result<ArgType[K]>) | K, options?: ArgOptions): Promise<ArgType[K]>;
public async peek<K extends keyof ArgType>(type: (() => Argument.Result<ArgType[K]>) | K, options?: ArgOptions): Promise<ArgType[K]> {
const result = await this.peekResult(type, options);
return result.unwrap();
return result.unwrapRaw();
}

/**
Expand Down

0 comments on commit ee8be2e

Please sign in to comment.