All files / lib/internal/process report.js

100% Statements 121/121
100% Branches 31/31
100% Functions 19/19
100% Lines 121/121

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 122140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 140x 19x 3x 3x 19x 13x 13x 15x 19x 6x 19x 9x 9x 10x 10x 140x 140x 13x 13x 13x 13x 13x 13x 140x 140x 6x 140x 140x 9x 9x 140x 140x 6x 140x 140x 3x 3x 140x 140x 22x 140x 140x 3x 3x 140x 140x 5x 140x 140x 6x 6x 6x 6x 140x 140x 6x 140x 140x 3x 3x 3x 140x 140x 6x 140x 140x 6x 6x 6x 6x 6x 140x 140x 8838x 140x 140x 27633x 27633x 27633x 27633x 140x 140x 4943x 4943x 7x 7x 7x 7x 7x 4943x 140x 8x 8x 8x 8x 8x 8x 140x 1x 1x 1x 140x 140x 140x 140x 140x  
'use strict';
const {
  ERR_SYNTHETIC
} = require('internal/errors').codes;
const {
  validateBoolean,
  validateObject,
  validateSignalName,
  validateString,
} = require('internal/validators');
const nr = internalBinding('report');
const {
  JSONParse,
} = primordials;
const report = {
  writeReport(file, err) {
    if (typeof file === 'object' && file !== null) {
      err = file;
      file = undefined;
    } else if (file !== undefined) {
      validateString(file, 'file');
    }
 
    if (err === undefined) {
      err = new ERR_SYNTHETIC();
    } else {
      validateObject(err, 'err');
    }
 
    return nr.writeReport('JavaScript API', 'API', file, err);
  },
  getReport(err) {
    if (err === undefined)
      err = new ERR_SYNTHETIC();
    else
      validateObject(err, 'err');
 
    return JSONParse(nr.getReport(err));
  },
  get directory() {
    return nr.getDirectory();
  },
  set directory(dir) {
    validateString(dir, 'directory');
    nr.setDirectory(dir);
  },
  get filename() {
    return nr.getFilename();
  },
  set filename(name) {
    validateString(name, 'filename');
    nr.setFilename(name);
  },
  get compact() {
    return nr.getCompact();
  },
  set compact(b) {
    validateBoolean(b, 'compact');
    nr.setCompact(b);
  },
  get signal() {
    return nr.getSignal();
  },
  set signal(sig) {
    validateSignalName(sig, 'signal');
    removeSignalHandler();
    addSignalHandler(sig);
    nr.setSignal(sig);
  },
  get reportOnFatalError() {
    return nr.shouldReportOnFatalError();
  },
  set reportOnFatalError(trigger) {
    validateBoolean(trigger, 'trigger');
 
    nr.setReportOnFatalError(trigger);
  },
  get reportOnSignal() {
    return nr.shouldReportOnSignal();
  },
  set reportOnSignal(trigger) {
    validateBoolean(trigger, 'trigger');
 
    nr.setReportOnSignal(trigger);
    removeSignalHandler();
    addSignalHandler();
  },
  get reportOnUncaughtException() {
    return nr.shouldReportOnUncaughtException();
  },
  set reportOnUncaughtException(trigger) {
    validateBoolean(trigger, 'trigger');
 
    nr.setReportOnUncaughtException(trigger);
  }
};
 
function addSignalHandler(sig) {
  if (nr.shouldReportOnSignal()) {
    if (typeof sig !== 'string')
      sig = nr.getSignal();
 
    process.on(sig, signalHandler);
  }
}
 
function removeSignalHandler() {
  const sig = nr.getSignal();
 
  if (sig)
    process.removeListener(sig, signalHandler);
}
 
function signalHandler(sig) {
  nr.writeReport(sig, 'Signal', null, '');
}
 
module.exports = {
  addSignalHandler,
  report
};