{"record":{"id":"1ca8a1dd036e823a","repo":"winstonjs/winston","slug":"logger-is-required-to-handle-rejections","errorCode":null,"errorMessage":"Logger is required to handle rejections","messagePattern":"Logger is required to handle rejections","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/winston/rejection-handler.js","lineNumber":28,"sourceCode":"const os = require('os');\nconst asyncForEach = require('async/forEach');\nconst debug = require('@dabh/diagnostics')('winston:rejection');\nconst once = require('one-time');\nconst stackTrace = require('stack-trace');\nconst RejectionStream = require('./rejection-stream');\n\n/**\n * Object for handling unhandledRejection events.\n * @type {RejectionHandler}\n */\nmodule.exports = class RejectionHandler {\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 rejections');\n    }\n\n    this.logger = logger;\n    this.handlers = new Map();\n  }\n\n  /**\n   * Handles `unhandledRejection` 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/rejection-handler.js#L10-L46","documentation":"RejectionHandler is the unhandled-rejection counterpart of ExceptionHandler: it captures unhandled promise rejections and routes them to a Logger's transports. Its constructor requires a Logger argument and throws when it is falsy, since a handler without a logger cannot record rejections.","triggerScenarios":"Calling `new winston.RejectionHandler()` with no argument or an undefined/null logger — e.g. from custom rejection plumbing instead of `logger.rejections.handle(...)`.","commonSituations":"Manual setup of unhandledRejection handling during app bootstrap before the logger exists; refactors that removed logger initialization; typos/shadowed logger variables.","solutions":["Pass an initialized logger: `new winston.RejectionHandler(logger)`.","Prefer `logger.rejections.handle(transport)`, which wires the handler correctly.","Order initialization so the logger is created before rejection handling is installed.","Validate the logger reference before constructing."],"exampleFix":"// before\nconst handler = new winston.RejectionHandler(loggerMaybeUndefined);\n\n// after\nif (!logger) throw new Error('Create logger before RejectionHandler');\nconst handler = new winston.RejectionHandler(logger);\n// or simply:\nlogger.rejections.handle(new winston.transports.File({ filename: 'rejections.log' }));","handlingStrategy":"type-guard","validationCode":"if (!logger || typeof logger.log !== 'function') {\n  throw new TypeError('RejectionHandler requires a winston Logger instance');\n}","typeGuard":"function isLogger(x) { return !!x && typeof x === 'object' && typeof x.log === 'function' && typeof x.rejections === 'object'; }","tryCatchPattern":"try {\n  handler = new winston.RejectionHandler(logger);\n} catch (err) {\n  if (err.message === 'Logger is required to handle rejections') {\n    logger.rejections.handle(fileTransport);\n  } else { throw err; }\n}","preventionTips":["Initialize the Logger before installing process-level rejection handling","Use `logger.rejections.handle(...)` instead of instantiating RejectionHandler directly","Centralize logger creation so handlers always receive a valid instance","Test bootstrap order in CI"],"tags":["winston","argument-validation","promise-rejections","constructor"],"backgroundTag":"missing-required-argument","analyzedSha":"ff0b79de8562bb322c390fbc82fe71c11f373428","analyzedAt":"2026-08-31T13:33:44.585Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}