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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | 7x 7x 7x 7x 7x 7x 7x 86x 1x 85x 85x 8x 3x 4x 8x 6x 6x 1x 1x 1x 1x 2x 5x 5x 1x 5x 6x 6x 7x 7x 53x 4x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 5x 3x 3x 3x 5x 5x 5x 5x 10x 5x 5x 5x 5x 5x 5x 3x 5x 5x 5x | /** * exception-handler.js: Object for handling uncaughtException events. * * (C) 2010 Charlie Robbins * MIT LICENCE */ 'use strict'; const os = require('os'); const asyncForEach = require('async/forEach'); const debug = require('diagnostics')('winston:exception'); const once = require('one-time'); const stackTrace = require('stack-trace'); const ExceptionStream = require('./exception-stream'); /** * Object for handling uncaughtException events. * @type {ExceptionHandler} */ module.exports = class ExceptionHandler { /** * TODO: add contructor description * @param {!Logger} logger - TODO: add param description */ constructor(logger) { if (!logger) { throw new Error('Logger is required to handle exceptions'); } this.logger = logger; this.handlers = new Map(); } /** * Handles `uncaughtException` events for the current process by adding any * handlers passed in. * @returns {undefined} */ handle(...args) { args.forEach(arg => { Eif (Array.isArray(arg)) { return arg.forEach(handler => this._addHandler(handler)); } this._addHandler(arg); }); if (!this.catcher) { this.catcher = this._uncaughtException.bind(this); process.on('uncaughtException', this.catcher); } } /** * Removes any handlers to `uncaughtException` events for the current * process. This does not modify the state of the `this.handlers` set. * @returns {undefined} */ unhandle() { Eif (this.catcher) { process.removeListener('uncaughtException', this.catcher); this.catcher = false; Array.from(this.handlers.values()) .forEach(wrapper => this.logger.unpipe(wrapper)); } } /** * TODO: add method description * @param {Error} err - Error to get information about. * @returns {mixed} - TODO: add return description. */ getAllInfo(err) { let { message } = err; if (!message && typeof err === 'string') { message = err; } return { error: err, // TODO (indexzero): how do we configure this? level: 'error', message: [ `uncaughtException: ${(message || '(no error message)')}`, err.stack || ' No stack trace' ].join('\n'), stack: err.stack, exception: true, date: new Date().toString(), process: this.getProcessInfo(), os: this.getOsInfo(), trace: this.getTrace(err) }; } /** * Gets all relevant process information for the currently running process. * @returns {mixed} - TODO: add return description. */ getProcessInfo() { return { pid: process.pid, uid: process.getuid ? process.getuid() : null, gid: process.getgid ? process.getgid() : null, cwd: process.cwd(), execPath: process.execPath, version: process.version, argv: process.argv, memoryUsage: process.memoryUsage() }; } /** * Gets all relevant OS information for the currently running process. * @returns {mixed} - TODO: add return description. */ getOsInfo() { return { loadavg: os.loadavg(), uptime: os.uptime() }; } /** * Gets a stack trace for the specified error. * @param {mixed} err - TODO: add param description. * @returns {mixed} - TODO: add return description. */ getTrace(err) { const trace = err ? stackTrace.parse(err) : stackTrace.get(); return trace.map(site => { return { column: site.getColumnNumber(), file: site.getFileName(), function: site.getFunctionName(), line: site.getLineNumber(), method: site.getMethodName(), native: site.isNative() }; }); } /** * Helper method to add a transport as an exception handler. * @param {Transport} handler - The transport to add as an exception handler. * @returns {void} */ _addHandler(handler) { Eif (!this.handlers.has(handler)) { handler.handleExceptions = true; const wrapper = new ExceptionStream(handler); this.handlers.set(handler, wrapper); this.logger.pipe(wrapper); } } /** * Logs all relevant information around the `err` and exits the current * process. * @param {Error} err - Error to handle * @returns {mixed} - TODO: add return description. * @private */ _uncaughtException(err) { const info = this.getAllInfo(err); const handlers = this._getExceptionHandlers(); // Calculate if we should exit on this error let doExit = typeof this.logger.exitOnError === 'function' ? this.logger.exitOnError(err) : this.logger.exitOnError; let timeout; Iif (!handlers.length && doExit) { // eslint-disable-next-line no-console console.warn('winston: exitOnError cannot be false with no exception handlers.'); // eslint-disable-next-line no-console console.warn('winston: exiting process.'); doExit = false; } function gracefulExit() { debug('doExit', doExit); debug('process._exiting', process._exiting); if (doExit && !process._exiting) { // Remark: Currently ignoring any exceptions from transports when // catching uncaught exceptions. Eif (timeout) { clearTimeout(timeout); } // eslint-disable-next-line no-process-exit process.exit(1); } } Iif (!handlers || handlers.length === 0) { return process.nextTick(gracefulExit); } // Log to all transports attempting to listen for when they are completed. asyncForEach(handlers, (handler, next) => { // TODO: Change these to the correct WritableStream events so that we // wait until exit. const done = once(next); const transport = handler.transport || handler; // Debug wrapping so that we can inspect what's going on under the covers. function onDone(event) { return () => { debug(event); done(); }; } transport.once('logged', onDone('logged')); transport.once('error', onDone('error')); }, gracefulExit); this.logger.log(info); // If exitOnError is true, then only allow the logging of exceptions to // take up to `3000ms`. if (doExit) { timeout = setTimeout(gracefulExit, 3000); } } /** * Returns the list of transports and exceptionHandlers for this instance. * @returns {Array} - List of transports and exceptionHandlers for this * instance. * @private */ _getExceptionHandlers() { // Remark (indexzero): since `logger.transports` returns all of the pipes // from the _readableState of the stream we actually get the join of the // explicit handlers and the implicit transports with // `handleExceptions: true` return this.logger.transports.filter(wrap => { const transport = wrap.transport || wrap; return transport.handleExceptions; }); } }; |