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
4 changes: 1 addition & 3 deletions src/13-catch-blocks.solution.2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ const tryCatchDemo = (state: "fail" | "succeed") => {
throw new Error("Failure!");
}
} catch (e) {
if (e instanceof Error) {
return e.message;
}
return (e as Error).message;
}
};

Expand Down
17 changes: 17 additions & 0 deletions src/13-catch-blocks.solution.3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect, it } from "vitest";

const tryCatchDemo = (state: "fail" | "succeed") => {
try {
if (state === "fail") {
throw new Error("Failure!");
}
} catch (e) {
if (e instanceof Error) {
return e.message;
}
}
};

it("Should return the message when it fails", () => {
expect(tryCatchDemo("fail")).toEqual("Failure!");
});