{"record":{"id":"14ba4379a787758f","repo":"winstonjs/winston","slug":"logger-is-required-to-handle-exceptions","errorCode":null,"errorMessage":"Logger is required to handle exceptions","messagePattern":"Logger is required to handle exceptions","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/winston/exception-handler.js","lineNumber":28,"sourceCode":"const os = require('os');\nconst asyncForEach = require('async/forEach');\nconst debug = require('@dabh/diagnostics')('winston:exception');\nconst once = require('one-time');\nconst stackTrace = require('stack-trace');\nconst ExceptionStream = require('./exception-stream');\n\n/**\n * Object for handling uncaughtException events.\n * @type {ExceptionHandler}\n */\nmodule.exports = class ExceptionHandler {\n  /**\n   * TODO: add contructor description\n   * @param {!Logger} logger - TODO: add param description\n   */\n  constructor(logger) {\n    if (!logger) {\n      throw new Error('Logger is required to handle exceptions');\n    }\n\n    this.logger = logger;\n    this.handlers = new Map();\n  }\n\n  /**\n   * Handles `uncaughtException` events for the current process by adding any\n   * handlers passed in.\n   * @returns {undefined}\n   */\n  handle(...args) {\n    args.forEach(arg => {\n      if (Array.isArray(arg)) {\n        return arg.forEach(handler => this._addHandler(handler));\n      }\n\n      this._addHandler(arg);","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/winstonjs/winston/blob/ff0b79de8562bb322c390fbc82fe71c11f373428/lib/winston/exception-handler.js#L10-L46","documentation":"ExceptionHandler wraps a Logger to catch uncaught exceptions and route them to the logger's transports. Its constructor requires the logger argument; throwing early when it is missing/undefined prevents creating a broken handler that could not dispatch exception events.","triggerScenarios":"Calling `new winston.ExceptionHandler()` with no argument, or with `undefined`/`null`/falsy value — typically from hand-rolled exception plumbing instead of using `logger.exceptions.handle(...)`.","commonSituations":"Manually instantiating ExceptionHandler in custom logging setups; refactoring code where the logger variable is not yet initialized when the handler is constructed; typos or shadowed logger variables.","solutions":["Construct the logger first and pass it: `new winston.ExceptionHandler(logger)`.","Prefer the public API `logger.exceptions.handle(transport)` which creates the handler for you with the correct logger.","Ensure the logger is created before any code that builds the exception handler (module initialization order).","Guard against undefined logger variables before constructing the handler."],"exampleFix":"// before\nconst handler = new winston.ExceptionHandler(loggerMaybeUndefined);\n\n// after\nif (!logger) throw new Error('Create logger before ExceptionHandler');\nconst handler = new winston.ExceptionHandler(logger);\n// or simply:\nlogger.exceptions.handle(new winston.transports.File({ filename: 'exceptions.log' }));","handlingStrategy":"type-guard","validationCode":"if (!logger || typeof logger.log !== 'function') {\n  throw new TypeError('ExceptionHandler requires a winston Logger instance');\n}","typeGuard":"function isLogger(x) { return !!x && typeof x === 'object' && typeof x.log === 'function' && typeof x.exceptions === 'object'; }","tryCatchPattern":"try {\n  handler = new winston.ExceptionHandler(logger);\n} catch (err) {\n  if (err.message === 'Logger is required to handle exceptions') {\n    handler = logger.exceptions.handle(fileTransport);\n  } else { throw err; }\n}","preventionTips":["Create the Logger at app bootstrap before wiring exception handling","Use `logger.exceptions.handle(...)` instead of instantiating ExceptionHandler directly","Check initialization order when splitting logger setup across modules","Add an integration test that enables exception handling at startup"],"tags":["winston","argument-validation","exceptions","constructor"],"backgroundTag":"missing-required-argument","analyzedSha":"ff0b79de8562bb322c390fbc82fe71c11f373428","analyzedAt":"2026-08-31T13:33:44.585Z","schemaVersion":2},"datasetVersion":"2026-09-01T08:17:40.651Z"}