From 61bc43edc24659a595a38b7da761a8a7a12fbca1 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 16 May 2023 13:52:30 +0200 Subject: [PATCH] refactor: move node unhandled errors to single place --- src/runtime/entries/nitro-prerenderer.ts | 18 +++--------------- src/runtime/entries/node-cluster.ts | 4 ++++ src/runtime/entries/node-server.ts | 18 +++--------------- src/runtime/entries/node.ts | 18 +++--------------- src/runtime/utils.ts | 18 ++++++++++++++++++ 5 files changed, 31 insertions(+), 45 deletions(-) diff --git a/src/runtime/entries/nitro-prerenderer.ts b/src/runtime/entries/nitro-prerenderer.ts index a17bdc4d3f..69618b3d4e 100644 --- a/src/runtime/entries/nitro-prerenderer.ts +++ b/src/runtime/entries/nitro-prerenderer.ts @@ -1,20 +1,8 @@ import "#internal/nitro/virtual/polyfill"; import { nitroApp } from "../app"; +import { trapUnhandledNodeErrors } from "../utils"; export const localFetch = nitroApp.localFetch; -if (process.env.DEBUG) { - process.on("unhandledRejection", (err) => - console.error("[nitro] [dev] [unhandledRejection]", err) - ); - process.on("uncaughtException", (err) => - console.error("[nitro] [dev] [uncaughtException]", err) - ); -} else { - process.on("unhandledRejection", (err) => - console.error("[nitro] [dev] [unhandledRejection] " + err) - ); - process.on("uncaughtException", (err) => - console.error("[nitro] [dev] [uncaughtException] " + err) - ); -} +// Trap unhandled errors +trapUnhandledNodeErrors(); diff --git a/src/runtime/entries/node-cluster.ts b/src/runtime/entries/node-cluster.ts index abe838b24b..700dac84ea 100644 --- a/src/runtime/entries/node-cluster.ts +++ b/src/runtime/entries/node-cluster.ts @@ -1,6 +1,7 @@ import os from "node:os"; import cluster from "node:cluster"; import { getGracefulShutdownConfig } from "../shutdown"; +import { trapUnhandledNodeErrors } from "../utils"; function runMaster() { const numberOfWorkers = @@ -66,6 +67,9 @@ function runWorker() { }); } +// Trap unhandled errors +trapUnhandledNodeErrors(); + if (cluster.isPrimary) { runMaster(); } else { diff --git a/src/runtime/entries/node-server.ts b/src/runtime/entries/node-server.ts index cc5cb8209b..3ad8aa6e22 100644 --- a/src/runtime/entries/node-server.ts +++ b/src/runtime/entries/node-server.ts @@ -6,6 +6,7 @@ import destr from "destr"; import { toNodeListener } from "h3"; import { nitroApp } from "../app"; import { setupGracefulShutdown } from "../shutdown"; +import { trapUnhandledNodeErrors } from "../utils"; import { useRuntimeConfig } from "#internal/nitro"; const cert = process.env.NITRO_SSL_CERT; @@ -38,21 +39,8 @@ const listener = server.listen(port, host, (err) => { console.log(`Listening ${url}`); }); -if (process.env.DEBUG) { - process.on("unhandledRejection", (err) => - console.error("[nitro] [dev] [unhandledRejection]", err) - ); - process.on("uncaughtException", (err) => - console.error("[nitro] [dev] [uncaughtException]", err) - ); -} else { - process.on("unhandledRejection", (err) => - console.error("[nitro] [dev] [unhandledRejection] " + err) - ); - process.on("uncaughtException", (err) => - console.error("[nitro] [dev] [uncaughtException] " + err) - ); -} +// Trap unhandled errors +trapUnhandledNodeErrors(); // Graceful shutdown setupGracefulShutdown(listener, nitroApp); diff --git a/src/runtime/entries/node.ts b/src/runtime/entries/node.ts index 83c3b62862..fc8acb8755 100644 --- a/src/runtime/entries/node.ts +++ b/src/runtime/entries/node.ts @@ -1,24 +1,12 @@ import "#internal/nitro/virtual/polyfill"; import { toNodeListener } from "h3"; import { nitroApp } from "../app"; +import { trapUnhandledNodeErrors } from "../utils"; export const listener = toNodeListener(nitroApp.h3App); /** @deprecated use new `listener` export instead */ export const handler = listener; -if (process.env.DEBUG) { - process.on("unhandledRejection", (err) => - console.error("[nitro] [dev] [unhandledRejection]", err) - ); - process.on("uncaughtException", (err) => - console.error("[nitro] [dev] [uncaughtException]", err) - ); -} else { - process.on("unhandledRejection", (err) => - console.error("[nitro] [dev] [unhandledRejection] " + err) - ); - process.on("uncaughtException", (err) => - console.error("[nitro] [dev] [uncaughtException] " + err) - ); -} +// Trap unhandled errors +trapUnhandledNodeErrors(); diff --git a/src/runtime/utils.ts b/src/runtime/utils.ts index a69e66a81a..3a6cd9d805 100644 --- a/src/runtime/utils.ts +++ b/src/runtime/utils.ts @@ -82,3 +82,21 @@ export function normalizeError(error: any) { message, }; } + +export function trapUnhandledNodeErrors() { + if (process.env.DEBUG) { + process.on("unhandledRejection", (err) => + console.error("[nitro] [unhandledRejection]", err) + ); + process.on("uncaughtException", (err) => + console.error("[nitro] [uncaughtException]", err) + ); + } else { + process.on("unhandledRejection", (err) => + console.error("[nitro] [unhandledRejection] " + err) + ); + process.on("uncaughtException", (err) => + console.error("[nitro] [uncaughtException] " + err) + ); + } +}