all files / lib/internal/process/ warning.js

82.86% Statements 29/35
81.82% Branches 27/33
100% Functions 2/2
87.5% Lines 28/32
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    1553× 1553× 1553× 1553× 1553×   1553×   1553× 1553× 1550× 51× 51× 51× 51× 51×   49× 49×   49×             1553× 58×       58× 55× 55× 55×   58×   56×     56×      
'use strict';
 
const traceWarnings = process.traceProcessWarnings;
const noDeprecation = process.noDeprecation;
const traceDeprecation = process.traceDeprecation;
const throwDeprecation = process.throwDeprecation;
const prefix = `(${process.release.name}:${process.pid}) `;
 
exports.setup = setupProcessWarnings;
 
function setupProcessWarnings() {
  if (!process.noProcessWarnings) {
    process.on('warning', (warning) => {
      Iif (!(warning instanceof Error)) return;
      const isDeprecation = warning.name === 'DeprecationWarning';
      Iif (isDeprecation && noDeprecation) return;
      const trace = traceWarnings || (isDeprecation && traceDeprecation);
      if (trace && warning.stack) {
        console.error(`${prefix}${warning.stack}`);
      } else {
        var toString = warning.toString;
        Iif (typeof toString !== 'function')
          toString = Error.prototype.toString;
        console.error(`${prefix}${toString.apply(warning)}`);
      }
    });
  }
 
  // process.emitWarning(error)
  // process.emitWarning(str[, name][, ctor])
  process.emitWarning = function(warning, name, ctor) {
    Iif (typeof name === 'function') {
      ctor = name;
      name = 'Warning';
    }
    if (warning === undefined || typeof warning === 'string') {
      warning = new Error(warning);
      warning.name = name || 'Warning';
      Error.captureStackTrace(warning, ctor || process.emitWarning);
    }
    if (!(warning instanceof Error)) {
      throw new TypeError('\'warning\' must be an Error object or string.');
    }
    Iif (throwDeprecation && warning.name === 'DeprecationWarning')
      throw warning;
    else
      process.nextTick(() => process.emit('warning', warning));
  };
}