opticore-catch-exception-error
v1.0.35
Published
opticore catch exception error
Maintainers
Readme
OptiCore Catch Exception Error
opticore catch exception error: logs the Node.js process events (uncaught exceptions, unhandled rejections, signals, exits, worker errors…) and handles Express errors for an OptiCore web server, with translated messages (en, fr).
Requirements
- Node.js >= 26.12.1
Install
npm i opticore-catch-exception-errorUsage
import EventEmitter from "node:events";
import { express } from "opticore-express";
import { ServerListenEventError, CEvent } from "opticore-catch-exception-error";
const events = new ServerListenEventError("en"); // language of the logged messages: "en" or "fr"
process.on(CEvent.uncaughtException, (error) => events.uncaughtException(error));
process.on(CEvent.unhandledRejection, (reason, promise) => events.unhandledRejection(reason, promise));
process.on(CEvent.warning, (warning) => events.warning(warning));
process.on(CEvent.sigterm, (signal) => events.sigtermSignalReceived(signal));
const app = express();
const errorEmitter = new EventEmitter();
errorEmitter.on("error", () => {});
// ... your routes ...
// last middleware: Express error handler
app.use((err, req, res, next) => events.expressErrorHandlingMiddleware(errorEmitter, err, req, res, next));The package also exports the event name constants CEvent, CEventNameError, CErrorName, CCodeError, the StackTraceError class and the error helpers AssertionErrors, InternalErrors, JavaScriptErrors, OpenSSLErrors, SystemErrors.
Security
Breaking changes
multipleResolvesis deprecated (Node.js DEP0160). Node.js 26 no longer emits themultipleResolvesprocess event, so a listener registered on it never runs.ServerListenEventError.multipleResolves()andCEvent.multipleResolvesare kept (marked@deprecated) so existing code still compiles and still works on an older runtime.expressErrorHandlingMiddleware: when the response has already started (res.headersSent), the error is handed to Express withnext(err)instead of trying to send a new status.
Behaviour
- Error listeners never throw.
null,undefined, primitives, Symbols, objects with anullprototype, circular objects, BigInts and values with a throwingtoStringare logged safely (throw null,Promise.reject(undefined),reject(null)…). Before, anullreason crashed the listener (Cannot read properties of null (reading 'toString')) and with it the whole process. - An error message equal to an
Object.prototypemember (constructor,__proto__,toString…) is not resolved as a translation key. - The Express error handler never sends error details to the client. A 4xx error raised by a middleware (malformed JSON, payload too large…:
err.status/err.statusCodebetween 400 and 499) keeps its status with the standard status text; anything else answers500with a fixed text (Internal Server Error/Erreur interne du serveur). The message, stack, SQL or file paths are only logged.
Warning: do not rely on
multipleResolveson Node.js 26 or later: it is never emitted. Detect double resolutions in your own code (e.g. guard theresolve/rejectcalls) if you need them.
Warning: there is no opt-out for the fixed error text: if a client needs details, send them yourself from your route before the error reaches this handler.
Security Issues
https://github.com/guyzoum77/opticore-catch-exception-error/issues
About
OptiCore Catch Exception Error is led by Guy-serge Kouacou.
