Skip to content
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

fix(Result.Err.unwrap): throw the original Error when possible #117

Merged
merged 1 commit into from
Apr 5, 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
9 changes: 4 additions & 5 deletions src/result/src/class/Err.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ export class ErrImpl<E> {
}

unwrap(): never {
const errorOptions = this.val instanceof Error ? { cause: this.val } : {};
if (this.val instanceof Error) {
throw this.val;
}

throw new Error(
`Tried to unwrap Error: ${toString(this.val)}\n${this._stack}`,
errorOptions
);
throw new Error(`Tried to unwrap Error: ${toString(this.val)}\n${this._stack}`);
}

map(_mapper: unknown): ErrImpl<E> {
Expand Down
31 changes: 19 additions & 12 deletions src/result/test/Result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ describe("Err", () => {
});

describe("unwrap", () => {
it("should throw when unwraping an Err value", () => {
it("should unwrap the original Error with no mutation", () => {
assert.throws(
() => Err("oops").unwrap(),
"oops"
() => Err(new Error("oops")).unwrap(),
{ message: "oops" }
);
});

it("should throw when unwraping an Err value and display the error cause (value is an Error)", () => {
it("should unwrap the origin CustomError with no mutation (and with the same Behavior as for a classical Error)", () => {
class CustomError extends Error {
foo: string;

Expand All @@ -109,7 +109,14 @@ describe("Err", () => {

assert.throws(
() => Err(new CustomError("oops")).unwrap(),
JSON.stringify({ foo: "bar" })
{ message: "oops" }
);
});

it("should wrap the literal value into a new Error", () => {
assert.throws(
() => Err(10).unwrap(),
{ message: /^Tried to unwrap Error: 10.*/g }
);
});
});
Expand All @@ -123,26 +130,26 @@ describe("Err", () => {
describe("map", () => {
it("should not map on Err value and keep original boxed Err", () => {
assert.throws(
() => Err("oops").map(() => 1).unwrap(),
"oops"
() => Err(new Error("oops")).map(() => 1).unwrap(),
{ message: "oops" }
);
});
});

describe("andThen", () => {
it("should not map/updated boxed value", () => {
assert.throws(
() => Err("oops").andThen(() => Ok(1)).unwrap(),
"oops"
() => Err(new Error("oops")).andThen(() => Ok(1)).unwrap(),
{ message: "oops" }
);
});
});

describe("mapErr", () => {
it("should map and return a new Err value", () => {
assert.throws(
() => Err("oops").mapErr(() => "oh no!").unwrap(),
"oh no!"
() => Err("oops").mapErr(() => new Error("oh no!")).unwrap(),
{ message: "oh no!" }
);
});
});
Expand All @@ -165,7 +172,7 @@ describe("Result", () => {
assert.ok(!result.ok);
assert.throws(
() => result.unwrap(),
"oops"
{ message: "oops" }
);
});
});
Expand Down