-
-
Save MarwanShehata/43ee9eb0320e04db6e2cc08f496b6436 to your computer and use it in GitHub Desktop.
A fixed version of an tryCatchWrapper for TypeScript.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Types for the result object with discriminated union | |
type Success<T> = { | |
data: T; | |
error: null; | |
}; | |
type Failure<E> = { | |
data: null; | |
error: E; | |
}; | |
type Result<T, E = Error> = Success<T> | Failure<E>; | |
// Unsound version of tryCatch | |
export async function tryCatchUnsound<T, E = Error>( | |
promise: Promise<T>, | |
): Promise<Result<T, E>> { | |
try { | |
const data = await promise; | |
return { data, error: null }; | |
} catch (error) { | |
return { data: null, error: error as E }; | |
} | |
} | |
// Fixed version of tryCatch | |
export async function tryCatchFixed<T, E = Error>( | |
fn: () => Promise<T>, | |
): Promise<Result<T, E>> { | |
try { | |
const data = await fn(); | |
return { data, error: null }; | |
} catch (error) { | |
return { data: null, error: error as E }; | |
} | |
} | |
// Example function that throws synchronously | |
function somePromiseThatThrowsSync(): Promise<string> { | |
throw new Error("Synchronous error!"); | |
return Promise.resolve("This will never run"); | |
} | |
// Example function that throws asynchronously | |
function somePromiseThatThrowsAsync(): Promise<string> { | |
return Promise.reject(new Error("Asynchronous error!")); | |
} | |
// Test the unsound version | |
async function testUnsound() { | |
console.log("Testing unsound version..."); | |
try { | |
const { data, error } = await tryCatchUnsound(somePromiseThatThrowsSync()); | |
if (error) { | |
console.error("Caught error (unsound):", error.message); | |
} else { | |
console.log("Data (unsound):", data); | |
} | |
} catch (err) { | |
// Narrow down the type of `err` before accessing `message` | |
if (err instanceof Error) { | |
console.error("Uncaught error (unsound):", err.message); | |
} else { | |
console.error("Uncaught unknown error (unsound):", err); | |
} | |
} | |
} | |
// Test the fixed version | |
async function testFixed() { | |
console.log("Testing fixed version..."); | |
try { | |
const { data, error } = await tryCatchFixed(() => somePromiseThatThrowsSync()); | |
if (error) { | |
console.error("Caught error (fixed):", error.message); | |
} else { | |
console.log("Data (fixed):", data); | |
} | |
} catch (err) { | |
// Narrow down the type of `err` before accessing `message` | |
if (err instanceof Error) { | |
console.error("Uncaught error (fixed):", err.message); | |
} else { | |
console.error("Uncaught unknown error (fixed):", err); | |
} | |
} | |
} | |
// Run tests | |
(async () => { | |
await testUnsound(); // This will crash with an uncaught error | |
await testFixed(); // This will catch the error properly | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment