Skip to content

Commit b3a6469

Browse files
committed
Add a custom UnwrapError type
1 parent 65a6588 commit b3a6469

File tree

7 files changed

+22
-6
lines changed

7 files changed

+22
-6
lines changed

src/Option/None.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Option, Some, some } from "./Option.ts";
22
import { Err, err } from "../Result/Result.ts";
33
import { OptionBase } from "./OptionBase.ts";
4+
import { UnwrapError } from "../UnwrapError/UnwrapError.ts";
45

56
export function none<T = never>(): None<T> {
67
return new None();
@@ -24,7 +25,7 @@ export class None<T = never> extends OptionBase<T> {
2425
}
2526

2627
unwrap(message: string = "Called Option#unwrap on a None value"): T {
27-
throw new Error(message);
28+
throw new UnwrapError(message, null);
2829
}
2930

3031
unwrapOr(defaultValue: T): T {

src/Result/Err.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Ok, Result } from "./Result.ts";
22
import { None, none, Some, some } from "../Option/Option.ts";
33
import { ResultBase } from "./ResultBase.ts";
4+
import { UnwrapError } from "../UnwrapError/UnwrapError.ts";
45

56
export function err<E>(error: E): Err<never, E> {
67
return new Err(error);
@@ -63,7 +64,7 @@ export class Err<T = never, E = unknown> extends ResultBase<T, E> {
6364
*[Symbol.iterator](): Iterator<T> {}
6465

6566
unwrap(message: string = `Expected ok() but got ${this}`): never {
66-
throw new Error(message);
67+
throw new UnwrapError(message, this.error);
6768
}
6869

6970
unwrapErr(_message: string = `Expected error but got ${this}`): E {

src/Result/Ok.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Err, Result } from "./Result.ts";
22
import { None, none, Some, some } from "../Option/Option.ts";
33
import { ResultBase } from "./ResultBase.ts";
4+
import { UnwrapError } from "../UnwrapError/UnwrapError.ts";
45

56
export function ok<T>(value: T): Ok<T> {
67
return new Ok(value);
@@ -69,7 +70,7 @@ export class Ok<T, E = never> extends ResultBase<T, E> {
6970
}
7071

7172
unwrapErr(message: string = `Expected error but got ${this}`): never {
72-
throw new Error(message);
73+
throw new UnwrapError(message, this.value);
7374
}
7475

7576
and<U>(resultB: Result<U, E>): Result<U, E> {

src/UnwrapError/UnwrapError.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export class UnwrapError<E = unknown> extends Error {
2+
error: E;
3+
4+
constructor(message: string, error: E) {
5+
super();
6+
7+
this.message = message;
8+
this.error = error;
9+
}
10+
}

tests/option_none_test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect, fn } from "@std/expect";
22
import { none, type Option, some } from "../main.ts";
3+
import { UnwrapError } from "../src/UnwrapError/UnwrapError.ts";
34

45
Deno.test("none() is None and not Some", () => {
56
const option = none();
@@ -24,7 +25,7 @@ Deno.test("none().isNoneOr(n) returns true", () => {
2425
Deno.test("none().unwrap() throws", () => {
2526
const option: Option<number> = none();
2627

27-
expect(() => option.unwrap()).toThrow();
28+
expect(() => option.unwrap()).toThrow(UnwrapError);
2829
});
2930

3031
Deno.test("none().unwrapOr(n) returns n", () => {

tests/result_err_test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from "@std/expect";
22
import { err, ok, type Result } from "../main.ts";
3+
import { UnwrapError } from "../src/UnwrapError/UnwrapError.ts";
34

45
Deno.test("err result is Err and not Ok", () => {
56
const result = err(42);
@@ -94,7 +95,7 @@ Deno.test({
9495
});
9596

9697
Deno.test("err().unwrap() throws", () => {
97-
expect(() => err("problem").unwrap()).toThrow();
98+
expect(() => err("problem").unwrap()).toThrow(UnwrapError);
9899
});
99100

100101
Deno.test("err(n).unwrapErr() returns n", () => {

tests/result_ok_test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from "@std/expect";
22
import { err, ok, type Result } from "../main.ts";
3+
import { UnwrapError } from "../src/UnwrapError/UnwrapError.ts";
34

45
Deno.test("OK result is Ok and not Err", () => {
56
const result = ok(42);
@@ -93,7 +94,7 @@ Deno.test("ok(n).unwrap() returns n", () => {
9394
});
9495

9596
Deno.test("ok().unwrapErr() throws", () => {
96-
expect(() => ok(42).unwrapErr()).toThrow();
97+
expect(() => ok(42).unwrapErr()).toThrow(UnwrapError);
9798
});
9899

99100
Deno.test("ok().and(b) returns b", () => {

0 commit comments

Comments
 (0)