• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const {
3  ERR_INVALID_ARG_TYPE,
4  ERR_SYNTHETIC
5} = require('internal/errors').codes;
6const {
7  validateSignalName,
8  validateString,
9  validateBoolean,
10} = require('internal/validators');
11const nr = internalBinding('report');
12const {
13  JSONParse,
14} = primordials;
15const report = {
16  writeReport(file, err) {
17    if (typeof file === 'object' && file !== null) {
18      err = file;
19      file = undefined;
20    } else if (file !== undefined && typeof file !== 'string') {
21      throw new ERR_INVALID_ARG_TYPE('file', 'String', file);
22    } else if (err === undefined) {
23      err = new ERR_SYNTHETIC();
24    } else if (err === null || typeof err !== 'object') {
25      throw new ERR_INVALID_ARG_TYPE('err', 'Object', err);
26    }
27
28    return nr.writeReport('JavaScript API', 'API', file, err);
29  },
30  getReport(err) {
31    if (err === undefined)
32      err = new ERR_SYNTHETIC();
33    else if (err === null || typeof err !== 'object')
34      throw new ERR_INVALID_ARG_TYPE('err', 'Object', err);
35
36    return JSONParse(nr.getReport(err));
37  },
38  get directory() {
39    return nr.getDirectory();
40  },
41  set directory(dir) {
42    validateString(dir, 'directory');
43    nr.setDirectory(dir);
44  },
45  get filename() {
46    return nr.getFilename();
47  },
48  set filename(name) {
49    validateString(name, 'filename');
50    nr.setFilename(name);
51  },
52  get compact() {
53    return nr.getCompact();
54  },
55  set compact(b) {
56    validateBoolean(b, 'compact');
57    nr.setCompact(b);
58  },
59  get signal() {
60    return nr.getSignal();
61  },
62  set signal(sig) {
63    validateSignalName(sig, 'signal');
64    removeSignalHandler();
65    addSignalHandler(sig);
66    nr.setSignal(sig);
67  },
68  get reportOnFatalError() {
69    return nr.shouldReportOnFatalError();
70  },
71  set reportOnFatalError(trigger) {
72    if (typeof trigger !== 'boolean')
73      throw new ERR_INVALID_ARG_TYPE('trigger', 'boolean', trigger);
74
75    nr.setReportOnFatalError(trigger);
76  },
77  get reportOnSignal() {
78    return nr.shouldReportOnSignal();
79  },
80  set reportOnSignal(trigger) {
81    if (typeof trigger !== 'boolean')
82      throw new ERR_INVALID_ARG_TYPE('trigger', 'boolean', trigger);
83
84    nr.setReportOnSignal(trigger);
85    removeSignalHandler();
86    addSignalHandler();
87  },
88  get reportOnUncaughtException() {
89    return nr.shouldReportOnUncaughtException();
90  },
91  set reportOnUncaughtException(trigger) {
92    if (typeof trigger !== 'boolean')
93      throw new ERR_INVALID_ARG_TYPE('trigger', 'boolean', trigger);
94
95    nr.setReportOnUncaughtException(trigger);
96  }
97};
98
99function addSignalHandler(sig) {
100  if (nr.shouldReportOnSignal()) {
101    if (typeof sig !== 'string')
102      sig = nr.getSignal();
103
104    process.on(sig, signalHandler);
105  }
106}
107
108function removeSignalHandler() {
109  const sig = nr.getSignal();
110
111  if (sig)
112    process.removeListener(sig, signalHandler);
113}
114
115function signalHandler(sig) {
116  nr.writeReport(sig, 'Signal', null, '');
117}
118
119module.exports = {
120  addSignalHandler,
121  report
122};
123