Skip to content

Commit

Permalink
Added expectErr()
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDudeFromCI authored and vultix committed Nov 12, 2022
1 parent 1e742ca commit d0fb55a
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ interface BaseResult<T, E> extends Iterable<T extends Iterable<infer U> ? U : ne
*/
expect(msg: string): T;

/**
* Returns the contained `Ok` value, if does not exist. Throws an error if it does.
* @param msg the message to throw if Ok value.
*/
expectErr(msg: string): T;

/**
* Returns the contained `Ok` value.
* Because this function may throw, its use is generally discouraged.
Expand Down Expand Up @@ -136,6 +142,10 @@ export class ErrImpl<E> implements BaseResult<never, E> {
throw new Error(`${msg} - Error: ${toString(this.val)}\n${this._stack}`);
}

expectErr(_msg: string): E {
return this.val
}

unwrap(): never {
throw new Error(`Tried to unwrap Error: ${toString(this.val)}\n${this._stack}`);
}
Expand Down Expand Up @@ -220,6 +230,10 @@ export class OkImpl<T> implements BaseResult<T, never> {
return this.val;
}

expectErr(msg: string): never {
throw new Error(msg);
}

unwrap(): T {
return this.val;
}
Expand Down

0 comments on commit d0fb55a

Please sign in to comment.