summaryrefslogtreecommitdiff
path: root/deps/npm/lib/utils/error-handler.js
blob: 5374d1feeca078aef26c23663686b3d428934555 (plain)
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
248
249
250
251
252
253
254
255
256
257
258

module.exports = errorHandler
module.exports.exit = exit

var cbCalled = false
var log = require('npmlog')
var npm = require('../npm.js')
var itWorked = false
var path = require('path')
var wroteLogFile = false
var exitCode = 0
var rollbacks = npm.rollbacks
var chain = require('slide').chain
var writeFileAtomic = require('write-file-atomic')
var errorMessage = require('./error-message.js')
var stopMetrics = require('./metrics.js').stop
var mkdirp = require('mkdirp')
var fs = require('graceful-fs')

var logFileName
function getLogFile () {
  if (!logFileName) {
    logFileName = path.resolve(npm.config.get('cache'), '_logs', (new Date()).toISOString().replace(/[.:]/g, '_') + '-debug.log')
  }
  return logFileName
}

var timings = {
  version: npm.version,
  command: process.argv.slice(2),
  logfile: null
}
process.on('timing', function (name, value) {
  if (timings[name]) { timings[name] += value } else { timings[name] = value }
})

process.on('exit', function (code) {
  process.emit('timeEnd', 'npm')
  log.disableProgress()
  if (npm.config.loaded && npm.config.get('timing')) {
    try {
      timings.logfile = getLogFile()
      fs.appendFileSync(path.join(npm.config.get('cache'), '_timing.json'), JSON.stringify(timings) + '\n')
    } catch (_) {
      // ignore
    }
  }

  // kill any outstanding stats reporter if it hasn't finished yet
  stopMetrics()

  if (code) itWorked = false
  if (itWorked) {
    log.info('ok')
  } else {
    if (!cbCalled) {
      log.error('', 'cb() never called!')
      console.error('')
      log.error('', 'This is an error with npm itself. Please report this error at:')
      log.error('', '    <https://github.com/npm/npm/issues>')
      writeLogFile()
    }

    if (code) {
      log.verbose('code', code)
    }
  }
  if (npm.config.loaded && npm.config.get('timing') && !wroteLogFile) writeLogFile()
  if (wroteLogFile) {
    // just a line break
    if (log.levels[log.level] <= log.levels.error) console.error('')

    log.error(
      '',
      [
        'A complete log of this run can be found in:',
        '    ' + getLogFile()
      ].join('\n')
    )
    wroteLogFile = false
  }

  var doExit = npm.config.loaded && npm.config.get('_exit')
  if (doExit) {
    // actually exit.
    if (exitCode === 0 && !itWorked) {
      exitCode = 1
    }
    if (exitCode !== 0) process.exit(exitCode)
  } else {
    itWorked = false // ready for next exit
  }
})

function exit (code, noLog) {
  exitCode = exitCode || process.exitCode || code

  var doExit = npm.config.loaded ? npm.config.get('_exit') : true
  log.verbose('exit', [code, doExit])
  if (log.level === 'silent') noLog = true

  if (rollbacks.length) {
    chain(rollbacks.map(function (f) {
      return function (cb) {
        npm.commands.unbuild([f], true, cb)
      }
    }), function (er) {
      if (er) {
        log.error('error rolling back', er)
        if (!code) {
          errorHandler(er)
        } else {
          if (!noLog) writeLogFile()
          reallyExit(er)
        }
      } else {
        if (!noLog && code) writeLogFile()
        reallyExit()
      }
    })
    rollbacks.length = 0
  } else if (code && !noLog) {
    writeLogFile(reallyExit)
  } else {
    reallyExit()
  }

  function reallyExit (er) {
    if (er && !code) code = typeof er.errno === 'number' ? er.errno : 1

    itWorked = !code

    // Exit directly -- nothing in the CLI should still be running in the
    // background at this point, and this makes sure anything left dangling
    // for whatever reason gets thrown away, instead of leaving the CLI open
    //
    // Commands that expect long-running actions should just delay `cb()`
    process.exit(code)
  }
}

function errorHandler (er) {
  log.disableProgress()
  if (!npm.config || !npm.config.loaded) {
    // logging won't work unless we pretend that it's ready
    er = er || new Error('Exit prior to config file resolving.')
    console.error(er.stack || er.message)
  }

  if (cbCalled) {
    er = er || new Error('Callback called more than once.')
  }

  cbCalled = true
  if (!er) return exit(0)
  if (typeof er === 'string') {
    log.error('', er)
    return exit(1, true)
  } else if (!(er instanceof Error)) {
    log.error('weird error', er)
    return exit(1, true)
  }

  var m = er.code || er.message.match(/^(?:Error: )?(E[A-Z]+)/)
  if (m && !er.code) {
    er.code = m
  }

  ;[
    'type',
    'fstream_path',
    'fstream_unc_path',
    'fstream_type',
    'fstream_class',
    'fstream_finish_call',
    'fstream_linkpath',
    'stack',
    'fstream_stack',
    'statusCode',
    'pkgid'
  ].forEach(function (k) {
    var v = er[k]
    if (!v) return
    if (k === 'fstream_stack') v = v.join('\n')
    log.verbose(k, v)
  })

  log.verbose('cwd', process.cwd())

  var os = require('os')
  log.verbose('', os.type() + ' ' + os.release())
  log.verbose('argv', process.argv.map(JSON.stringify).join(' '))
  log.verbose('node', process.version)
  log.verbose('npm ', 'v' + npm.version)

  ;[
    'file',
    'path',
    'code',
    'errno',
    'syscall'
  ].forEach(function (k) {
    var v = er[k]
    if (v) log.error(k, v)
  })

  var msg = errorMessage(er)
  msg.summary.concat(msg.detail).forEach(function (errline) {
    log.error.apply(log, errline)
  })
  if (npm.config.get('json')) {
    var error = {
      error: {
        code: er.code,
        summary: messageText(msg.summary),
        detail: messageText(msg.detail)
      }
    }
    console.log(JSON.stringify(error, null, 2))
  }

  exit(typeof er.errno === 'number' ? er.errno : 1)
}

function messageText (msg) {
  return msg.map(function (line) {
    return line.slice(1).join(' ')
  }).join('\n')
}

function writeLogFile () {
  if (wroteLogFile) return

  var os = require('os')

  try {
    mkdirp.sync(path.resolve(npm.config.get('cache'), '_logs'))
    var logOutput = ''
    log.record.forEach(function (m) {
      var pref = [m.id, m.level]
      if (m.prefix) pref.push(m.prefix)
      pref = pref.join(' ')

      m.message.trim().split(/\r?\n/).map(function (line) {
        return (pref + ' ' + line).trim()
      }).forEach(function (line) {
        logOutput += line + os.EOL
      })
    })
    writeFileAtomic.sync(getLogFile(), logOutput)

    // truncate once it's been written.
    log.record.length = 0
    wroteLogFile = true
  } catch (ex) {
    return
  }
}