All files / lib/internal/process warning.js

91.75% Statements 178/194
91.78% Branches 67/73
90% Functions 9/10
91.75% Lines 178/194

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 19523x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x                 23x 582x 582x 582x 582x 446x 446x     446x 2x 446x 444x 444x 446x 582x 582x 23x 23x 23x 23x 580x 580x 444x 444x 580x 580x 23x 2x 2x 2x 2x     2x 23x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x     2x 2x 23x 715x 715x 715x 23x 583x 583x 582x 583x 582x 583x 583x 583x 583x 583x 6x 583x 576x 576x 576x 576x 576x 583x 1x 1x 583x 442x 442x 442x 442x 442x 442x 582x 583x 2x 2x 580x 583x 23x 23x 23x 23x 752x 752x 752x 9x 9x 9x 9x 9x 752x 1x 1x 1x 1x 752x 752x 752x 9x 9x 752x 207x 207x 752x 689x 744x 7x 7x 752x 216x 216x 215x 4x 4x 4x 4x 4x 4x 216x 730x 752x 23x 7x 7x 7x 23x 696x 696x 696x 696x 696x 696x 696x 696x 696x 696x 696x 696x 696x 696x 696x 23x 23x 23x 23x 23x 23x 23x  
'use strict';
 
const {
  ArrayIsArray,
  Error,
  ErrorPrototypeToString,
  ErrorCaptureStackTrace,
  String,
} = primordials;
 
const assert = require('internal/assert');
const {
  codes: {
    ERR_INVALID_ARG_TYPE,
  },
  isErrorStackTraceLimitWritable,
} = require('internal/errors');
const { validateString } = require('internal/validators');
 
// Lazily loaded
let fs;
let fd;
let warningFile;
let options;
let traceWarningHelperShown = false;
 
function resetForSerialization() {
  if (fd !== undefined) {
    process.removeListener('exit', closeFdOnExit);
  }
  fd = undefined;
  warningFile = undefined;
  traceWarningHelperShown = false;
}
 
function lazyOption() {
  // This will load `warningFile` only once. If the flag is not set,
  // `warningFile` will be set to an empty string.
  if (warningFile === undefined) {
    options = require('internal/options');
    if (options.getOptionValue('--diagnostic-dir') !== '') {
      warningFile = options.getOptionValue('--diagnostic-dir');
    }
    if (options.getOptionValue('--redirect-warnings') !== '') {
      warningFile = options.getOptionValue('--redirect-warnings');
    } else {
      warningFile = '';
    }
  }
  return warningFile;
}
 
// If we can't write to stderr, we'd like to make this a noop,
// so use console.error.
let error;
function writeOut(message) {
  if (!error) {
    error = require('internal/console/global').error;
  }
  error(message);
}
 
function closeFdOnExit() {
  try {
    fs.closeSync(fd);
  } catch {
    // Continue regardless of error.
  }
}
 
function writeToFile(message) {
  if (fd === undefined) {
    fs = require('fs');
    try {
      fd = fs.openSync(warningFile, 'a');
    } catch {
      return writeOut(message);
    }
    process.on('exit', closeFdOnExit);
  }
  fs.appendFile(fd, `${message}\n`, (err) => {
    if (err) {
      writeOut(message);
    }
  });
}
 
function doEmitWarning(warning) {
  process.emit('warning', warning);
}
 
function onWarning(warning) {
  if (!(warning instanceof Error)) return;
  const isDeprecation = warning.name === 'DeprecationWarning';
  if (isDeprecation && process.noDeprecation) return;
  const trace = process.traceProcessWarnings ||
                (isDeprecation && process.traceDeprecation);
  let msg = `(${process.release.name}:${process.pid}) `;
  if (warning.code)
    msg += `[${warning.code}] `;
  if (trace && warning.stack) {
    msg += `${warning.stack}`;
  } else {
    msg +=
      typeof warning.toString === 'function' ?
        `${warning.toString()}` :
        ErrorPrototypeToString(warning);
  }
  if (typeof warning.detail === 'string') {
    msg += `\n${warning.detail}`;
  }
  if (!trace && !traceWarningHelperShown) {
    const flag = isDeprecation ? '--trace-deprecation' : '--trace-warnings';
    const argv0 = require('path').basename(process.argv0 || 'node', '.exe');
    msg += `\n(Use \`${argv0} ${flag} ...\` to show where the warning ` +
           'was created)';
    traceWarningHelperShown = true;
  }
  const warningFile = lazyOption();
  if (warningFile) {
    return writeToFile(msg);
  }
  writeOut(msg);
}
 
// process.emitWarning(error)
// process.emitWarning(str[, type[, code]][, ctor])
// process.emitWarning(str[, options])
function emitWarning(warning, type, code, ctor) {
  let detail;
  if (type !== null && typeof type === 'object' && !ArrayIsArray(type)) {
    ctor = type.ctor;
    code = type.code;
    if (typeof type.detail === 'string')
      detail = type.detail;
    type = type.type || 'Warning';
  } else if (typeof type === 'function') {
    ctor = type;
    code = undefined;
    type = 'Warning';
  }
  if (type !== undefined)
    validateString(type, 'type');
  if (typeof code === 'function') {
    ctor = code;
    code = undefined;
  } else if (code !== undefined) {
    validateString(code, 'code');
  }
  if (typeof warning === 'string') {
    warning = createWarningObject(warning, type, code, ctor, detail);
  } else if (!(warning instanceof Error)) {
    throw new ERR_INVALID_ARG_TYPE('warning', ['Error', 'string'], warning);
  }
  if (warning.name === 'DeprecationWarning') {
    if (process.noDeprecation)
      return;
    if (process.throwDeprecation) {
      // Delay throwing the error to guarantee that all former warnings were
      // properly logged.
      return process.nextTick(() => {
        throw warning;
      });
    }
  }
  process.nextTick(doEmitWarning, warning);
}
 
function emitWarningSync(warning) {
  process.emit('warning', createWarningObject(warning));
}
 
function createWarningObject(warning, type, code, ctor, detail) {
  assert(typeof warning === 'string');
  // Improve error creation performance by skipping the error frames.
  // They are added in the `captureStackTrace()` function below.
  const tmpStackLimit = Error.stackTraceLimit;
  if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0;
  // eslint-disable-next-line no-restricted-syntax
  warning = new Error(warning);
  if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = tmpStackLimit;
  warning.name = String(type || 'Warning');
  if (code !== undefined) warning.code = code;
  if (detail !== undefined) warning.detail = detail;
  ErrorCaptureStackTrace(warning, ctor || process.emitWarning);
  return warning;
}
 
module.exports = {
  emitWarning,
  emitWarningSync,
  onWarning,
  resetForSerialization,
};