From 72d52d925677eeeac6d158028114201862b6e2a2 Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Sun, 23 Mar 2025 10:05:51 -0500 Subject: [PATCH] feat(trycatch): added in theo's try catch to reduce the code and love it --- server/globalUtils/tryCatch.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 server/globalUtils/tryCatch.ts diff --git a/server/globalUtils/tryCatch.ts b/server/globalUtils/tryCatch.ts new file mode 100644 index 0000000..200d6ad --- /dev/null +++ b/server/globalUtils/tryCatch.ts @@ -0,0 +1,24 @@ +// Types for the result object with discriminated union +type Success = { + data: T; + error: null; +}; + +type Failure = { + data: null; + error: E; +}; + +type Result = Success | Failure; + +// Main wrapper function +export async function tryCatch( + promise: Promise +): Promise> { + try { + const data = await promise; + return { data, error: null }; + } catch (error) { + return { data: null, error: error as E }; + } +}