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 }; + } +}