From 8b218eb1887c11ac1ab18d0ba6b6d5fe51149f33 Mon Sep 17 00:00:00 2001 From: itsAlif Date: Sat, 27 Aug 2022 14:49:06 +0600 Subject: [PATCH 1/2] Solved catch block problem --- src/13-catch-blocks.solution.3.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/13-catch-blocks.solution.3.ts diff --git a/src/13-catch-blocks.solution.3.ts b/src/13-catch-blocks.solution.3.ts new file mode 100644 index 00000000..c3ca74d5 --- /dev/null +++ b/src/13-catch-blocks.solution.3.ts @@ -0,0 +1,15 @@ +import { expect, it } from "vitest"; + +const tryCatchDemo = (state: "fail" | "succeed") => { + try { + if (state === "fail") { + throw new Error("Failure!"); + } + } catch (e) { + return (e as Error).message; + } +}; + +it("Should return the message when it fails", () => { + expect(tryCatchDemo("fail")).toEqual("Failure!"); +}); From 6e76f1cdeafb7b7c39b49529fc901161a8d86caa Mon Sep 17 00:00:00 2001 From: itsAlif Date: Sat, 27 Aug 2022 15:46:45 +0600 Subject: [PATCH 2/2] catch block sloution reorderd --- src/13-catch-blocks.solution.2.ts | 4 +--- src/13-catch-blocks.solution.3.ts | 4 +++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/13-catch-blocks.solution.2.ts b/src/13-catch-blocks.solution.2.ts index c47d9362..c3ca74d5 100644 --- a/src/13-catch-blocks.solution.2.ts +++ b/src/13-catch-blocks.solution.2.ts @@ -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; } }; diff --git a/src/13-catch-blocks.solution.3.ts b/src/13-catch-blocks.solution.3.ts index c3ca74d5..c47d9362 100644 --- a/src/13-catch-blocks.solution.3.ts +++ b/src/13-catch-blocks.solution.3.ts @@ -6,7 +6,9 @@ const tryCatchDemo = (state: "fail" | "succeed") => { throw new Error("Failure!"); } } catch (e) { - return (e as Error).message; + if (e instanceof Error) { + return e.message; + } } };