"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/utils/error-handler.js" (11 Apr 2017, 5114 Bytes) of package /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 
    2 module.exports = errorHandler
    3 
    4 var cbCalled = false
    5 var log = require('npmlog')
    6 var npm = require('../npm.js')
    7 var rm = require('rimraf')
    8 var itWorked = false
    9 var path = require('path')
   10 var wroteLogFile = false
   11 var exitCode = 0
   12 var rollbacks = npm.rollbacks
   13 var chain = require('slide').chain
   14 var writeStreamAtomic = require('fs-write-stream-atomic')
   15 var errorMessage = require('./error-message.js')
   16 
   17 process.on('exit', function (code) {
   18   log.disableProgress()
   19   if (!npm.config || !npm.config.loaded) return
   20   if (code) itWorked = false
   21   if (itWorked) log.info('ok')
   22   else {
   23     if (!cbCalled) {
   24       log.error('', 'cb() never called!')
   25     }
   26 
   27     if (wroteLogFile) {
   28       // just a line break
   29       if (log.levels[log.level] <= log.levels.error) console.error('')
   30 
   31       log.error(
   32         '',
   33         [
   34           'Please include the following file with any support request:',
   35           '    ' + path.resolve('npm-debug.log')
   36         ].join('\n')
   37       )
   38       wroteLogFile = false
   39     }
   40     if (code) {
   41       log.error('code', code)
   42     }
   43   }
   44 
   45   var doExit = npm.config.get('_exit')
   46   if (doExit) {
   47     // actually exit.
   48     if (exitCode === 0 && !itWorked) {
   49       exitCode = 1
   50     }
   51     if (exitCode !== 0) process.exit(exitCode)
   52   } else {
   53     itWorked = false // ready for next exit
   54   }
   55 })
   56 
   57 function exit (code, noLog) {
   58   exitCode = exitCode || process.exitCode || code
   59 
   60   var doExit = npm.config ? npm.config.get('_exit') : true
   61   log.verbose('exit', [code, doExit])
   62   if (log.level === 'silent') noLog = true
   63 
   64   if (rollbacks.length) {
   65     chain(rollbacks.map(function (f) {
   66       return function (cb) {
   67         npm.commands.unbuild([f], true, cb)
   68       }
   69     }), function (er) {
   70       if (er) {
   71         log.error('error rolling back', er)
   72         if (!code) errorHandler(er)
   73         else if (noLog) rm('npm-debug.log', reallyExit.bind(null, er))
   74         else writeLogFile(reallyExit.bind(this, er))
   75       } else {
   76         if (!noLog && code) writeLogFile(reallyExit)
   77         else rm('npm-debug.log', reallyExit)
   78       }
   79     })
   80     rollbacks.length = 0
   81   } else if (code && !noLog) {
   82     writeLogFile(reallyExit)
   83   } else {
   84     rm('npm-debug.log', reallyExit)
   85   }
   86 
   87   function reallyExit (er) {
   88     if (er && !code) code = typeof er.errno === 'number' ? er.errno : 1
   89 
   90     // truncate once it's been written.
   91     log.record.length = 0
   92 
   93     itWorked = !code
   94 
   95     // just emit a fake exit event.
   96     // if we're really exiting, then let it exit on its own, so that
   97     // in-process stuff can finish or clean up first.
   98     if (!doExit) process.emit('exit', code)
   99   }
  100 }
  101 
  102 function errorHandler (er) {
  103   log.disableProgress()
  104   // console.error('errorHandler', er)
  105   if (!npm.config || !npm.config.loaded) {
  106     // logging won't work unless we pretend that it's ready
  107     er = er || new Error('Exit prior to config file resolving.')
  108     console.error(er.stack || er.message)
  109   }
  110 
  111   if (cbCalled) {
  112     er = er || new Error('Callback called more than once.')
  113   }
  114 
  115   cbCalled = true
  116   if (!er) return exit(0)
  117   if (typeof er === 'string') {
  118     log.error('', er)
  119     return exit(1, true)
  120   } else if (!(er instanceof Error)) {
  121     log.error('weird error', er)
  122     return exit(1, true)
  123   }
  124 
  125   var m = er.code || er.message.match(/^(?:Error: )?(E[A-Z]+)/)
  126   if (m && !er.code) {
  127     er.code = m
  128   }
  129 
  130   ;[
  131     'type',
  132     'fstream_path',
  133     'fstream_unc_path',
  134     'fstream_type',
  135     'fstream_class',
  136     'fstream_finish_call',
  137     'fstream_linkpath',
  138     'stack',
  139     'fstream_stack',
  140     'statusCode',
  141     'pkgid'
  142   ].forEach(function (k) {
  143     var v = er[k]
  144     if (!v) return
  145     if (k === 'fstream_stack') v = v.join('\n')
  146     log.verbose(k, v)
  147   })
  148 
  149   log.verbose('cwd', process.cwd())
  150 
  151   var os = require('os')
  152   // log.error('System', os.type() + ' ' + os.release())
  153   // log.error('command', process.argv.map(JSON.stringify).join(' '))
  154   // log.error('node -v', process.version)
  155   // log.error('npm -v', npm.version)
  156   log.error('', os.type() + ' ' + os.release())
  157   log.error('argv', process.argv.map(JSON.stringify).join(' '))
  158   log.error('node', process.version)
  159   log.error('npm ', 'v' + npm.version)
  160 
  161   ;[
  162     'file',
  163     'path',
  164     'code',
  165     'errno',
  166     'syscall'
  167   ].forEach(function (k) {
  168     var v = er[k]
  169     if (v) log.error(k, v)
  170   })
  171 
  172   // just a line break
  173   if (log.levels[log.level] <= log.levels.error) console.error('')
  174 
  175   var msg = errorMessage(er)
  176   msg.summary.concat(msg.detail).forEach(function (errline) {
  177     log.error.apply(log, errline)
  178   })
  179 
  180   exit(typeof er.errno === 'number' ? er.errno : 1)
  181 }
  182 
  183 var writingLogFile = false
  184 function writeLogFile (cb) {
  185   if (writingLogFile) return cb()
  186   writingLogFile = true
  187   wroteLogFile = true
  188 
  189   var fstr = writeStreamAtomic('npm-debug.log')
  190   var os = require('os')
  191   var out = ''
  192 
  193   log.record.forEach(function (m) {
  194     var pref = [m.id, m.level]
  195     if (m.prefix) pref.push(m.prefix)
  196     pref = pref.join(' ')
  197 
  198     m.message.trim().split(/\r?\n/).map(function (line) {
  199       return (pref + ' ' + line).trim()
  200     }).forEach(function (line) {
  201       out += line + os.EOL
  202     })
  203   })
  204 
  205   fstr.end(out)
  206   fstr.on('close', cb)
  207 }