Skip to content
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
29 changes: 29 additions & 0 deletions packages/shared/src/__tests__/result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ describe("tryCatchIf", () => {
}),
).toThrow("unexpected");
});

it("re-throws non-Error values as normalized Error instances", () => {
const guard = () => false;
// Wrap in tryCatch to capture the re-thrown value without raw try/catch
const result = tryCatch(() =>
tryCatchIf(guard, () => {
throw "raw string error";
}),
);
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toBeInstanceOf(Error);
expect(result.error.message).toBe("raw string error");
}
});
});

describe("asyncTryCatchIf", () => {
Expand Down Expand Up @@ -167,6 +182,20 @@ describe("asyncTryCatchIf", () => {
}),
).rejects.toThrow("unexpected");
});

it("re-throws non-Error values as normalized Error instances", async () => {
const guard = () => false;
const result = await asyncTryCatch(() =>
asyncTryCatchIf(guard, async () => {
throw 404;
}),
);
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error).toBeInstanceOf(Error);
expect(result.error.message).toBe("404");
}
});
});

describe("unwrapOr", () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function tryCatchIf<T>(guard: (err: Error) => boolean, fn: () => T): Resu
if (guard(err)) {
return Err(err);
}
throw e;
throw err;
}
}

Expand All @@ -70,7 +70,7 @@ export async function asyncTryCatchIf<T>(guard: (err: Error) => boolean, fn: ()
if (guard(err)) {
return Err(err);
}
throw e;
throw err;
}
}

Expand Down
Loading