Skip to content

Commit

Permalink
refactor: move node unhandled errors to single place
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed May 16, 2023
1 parent 6a9c6b9 commit 61bc43e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 45 deletions.
18 changes: 3 additions & 15 deletions src/runtime/entries/nitro-prerenderer.ts
Original file line number Diff line number Diff line change
@@ -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();
4 changes: 4 additions & 0 deletions src/runtime/entries/node-cluster.ts
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -66,6 +67,9 @@ function runWorker() {
});
}

// Trap unhandled errors
trapUnhandledNodeErrors();

if (cluster.isPrimary) {
runMaster();
} else {
Expand Down
18 changes: 3 additions & 15 deletions src/runtime/entries/node-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 3 additions & 15 deletions src/runtime/entries/node.ts
Original file line number Diff line number Diff line change
@@ -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();
18 changes: 18 additions & 0 deletions src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}

0 comments on commit 61bc43e

Please sign in to comment.