Last active
May 5, 2022 03:30
-
-
Save cardin/01c2ad32674425ebeaf3d3b63fb6aac4 to your computer and use it in GitHub Desktop.
Demonstrates NodeJS UncaughtException and UnhandledRejection
This file contains 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
#! /usr/bin/env node | |
/* | |
npm install p-queue | |
*/ | |
import PQueue from "p-queue"; | |
process.on("uncaughtException", (err) => { | |
console.log("Uncaught!", err); | |
}); | |
process.on("unhandledRejection", (err) => { | |
console.log("Unhandled!", err); | |
}); | |
// -------------------------------------------------- | |
// Direct Error | |
// -------------------------------------------------- | |
// throw new Error(); // uncaught | |
// -------------------------------------------------- | |
// Promise that Throws | |
// -------------------------------------------------- | |
const promiseThrow = () => | |
new Promise((_, rej) => { | |
throw new Error("Thrown Error"); | |
}); | |
// promiseThrow(); // unhandled | |
// await promiseThrow(); // uncaught | |
promiseThrow().catch(() => {}); | |
await promiseThrow().catch(() => {}); | |
// try { promiseThrow() } catch {} // unhandled | |
try { | |
await promiseThrow(); | |
} catch {} | |
// -------------------------------------------------- | |
// Promise that Rejects | |
// -------------------------------------------------- | |
const promiseRejectStr = () => Promise.reject("Reject Str"); | |
// promiseRejectStr(); // unhandled | |
// await promiseRejectStr(); // uncaught | |
promiseRejectStr().catch(() => {}); | |
await promiseRejectStr().catch(() => {}); | |
// try { promiseRejectStr() } catch {} // unhandled | |
try { | |
await promiseRejectStr(); | |
} catch {} | |
// -------------------------------------------------- | |
// Catch that Throws | |
// -------------------------------------------------- | |
const promiseCatchRethrow = () => | |
Promise.reject("Catch Rethrow").catch((err) => { | |
throw err; | |
}); | |
// promiseCatchThrow(); // unhandled | |
// await promiseCatchThrow(); // uncaught | |
const promiseCatchThrow = () => | |
Promise.reject("Catch Throw").catch((err) => { | |
throw new Error(); | |
}); | |
// promiseCatchThrow(); // unhandled | |
// await promiseCatchThrow(); // uncaught | |
// -------------------------------------------------- | |
// PQueue Rejection | |
// -------------------------------------------------- | |
const pqueue = new PQueue(); | |
const pqueueReject = () => Promise.reject("pqueue reject"); | |
// pqueue.add(pqueueReject); // unhandled | |
// await pqueue.add(pqueueReject); // uncaught | |
pqueue.add(pqueueReject).catch(() => undefined); | |
try { | |
await pqueue.add(pqueueReject); | |
} catch {} | |
async function pqueueRejectFunc() { | |
try { | |
return pqueue.add(pqueueReject); | |
} catch (err) { | |
console.error("Caught by function", err); | |
} | |
} | |
// pqueueRejectFunc(); // unhandled | |
async function pqueueRejectAwaitFunc() { | |
try { | |
return await pqueue.add(pqueueReject); | |
} catch (err) { | |
console.error("Caught by function", err); | |
} | |
} | |
pqueueRejectAwaitFunc(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment