PageRenderTime 64ms CodeModel.GetById 29ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 1ms

/node-v0.8.11/deps/npm/lib/utils/error-handler.js

https://bitbucket.org/jmchen/pysource
JavaScript | 293 lines | 246 code | 38 blank | 9 comment | 44 complexity | e7e1e7e8d0e7dc12431667c0fa6ed612 MD5 | raw file
  1
  2module.exports = errorHandler
  3
  4var cbCalled = false
  5  , log = require("npmlog")
  6  , npm = require("../npm.js")
  7  , rm = require("rimraf")
  8  , itWorked = false
  9  , path = require("path")
 10  , wroteLogFile = false
 11  , exitCode = 0
 12
 13
 14process.on("exit", function (code) {
 15  // console.error("exit", code)
 16  if (!npm.config.loaded) return
 17  if (code) itWorked = false
 18  if (itWorked) log.info("ok")
 19  else {
 20    if (!cbCalled) {
 21      log.error("", "cb() never called!")
 22    }
 23
 24    if (wroteLogFile) {
 25      log.error("", [""
 26                ,"Additional logging details can be found in:"
 27                ,"    " + path.resolve("npm-debug.log")
 28                ].join("\n"))
 29      wroteLogFile = false
 30    }
 31    log.error("not ok", "code", code)
 32  }
 33
 34  var doExit = npm.config.get("_exit")
 35  if (doExit) {
 36    // actually exit.
 37    if (exitCode === 0 && !itWorked) {
 38      exitCode = 1
 39    }
 40    if (exitCode !== 0) process.exit(exitCode)
 41  } else {
 42    itWorked = false // ready for next exit
 43  }
 44})
 45
 46function exit (code, noLog) {
 47  exitCode = exitCode || code
 48
 49  var doExit = npm.config.get("_exit")
 50  log.verbose("exit", [code, doExit])
 51  if (log.level === "silent") noLog = true
 52
 53  if (code && !noLog) writeLogFile(reallyExit)
 54  else rm("npm-debug.log", function () { rm(npm.tmp, reallyExit) })
 55
 56  function reallyExit() {
 57    // truncate once it's been written.
 58    log.record.length = 0
 59
 60    itWorked = !code
 61
 62    // just emit a fake exit event.
 63    // if we're really exiting, then let it exit on its own, so that
 64    // in-process stuff can finish or clean up first.
 65    if (!doExit) process.emit("exit", code)
 66  }
 67}
 68
 69
 70function errorHandler (er) {
 71  var printStack = false
 72  // console.error("errorHandler", er)
 73  if (!npm.config.loaded) {
 74    // logging won't work unless we pretend that it's ready
 75    er = er || new Error("Exit prior to config file resolving.")
 76    console.error(er.stack || er.message)
 77  }
 78
 79  if (cbCalled) {
 80    er = er || new Error("Callback called more than once.")
 81  }
 82
 83  cbCalled = true
 84  if (!er) return exit(0)
 85  if (typeof er === "string") {
 86    log.error("", er)
 87    return exit(1, true)
 88  } else if (!(er instanceof Error)) {
 89    log.error("weird error", er)
 90    return exit(1, true)
 91  }
 92
 93  var m = er.code || er.message.match(/^(?:Error: )?(E[A-Z]+)/)
 94  if (m && !er.code) er.code = m
 95
 96  switch (er.code) {
 97  case "ECONNREFUSED":
 98    log.error("", er)
 99    log.error("", ["\nIf you are behind a proxy, please make sure that the"
100              ,"'proxy' config is set properly.  See: 'npm help config'"
101              ].join("\n"))
102    printStack = true
103    break
104
105  case "EACCES":
106  case "EPERM":
107    log.error("", er)
108    log.error("", ["\nPlease try running this command again as root/Administrator."
109              ].join("\n"))
110    printStack = true
111    break
112
113  case "ELIFECYCLE":
114    er.code = "ELIFECYCLE"
115    log.error("", er.message)
116    log.error("", ["","Failed at the "+er.pkgid+" "+er.stage+" script."
117              ,"This is most likely a problem with the "+er.pkgname+" package,"
118              ,"not with npm itself."
119              ,"Tell the author that this fails on your system:"
120              ,"    "+er.script
121              ,"You can get their info via:"
122              ,"    npm owner ls "+er.pkgname
123              ,"There is likely additional logging output above."
124              ].join("\n"))
125    break
126
127  case "EJSONPARSE":
128    er.code = "EJSONPARSE"
129    log.error("", er.message)
130    log.error("", "File: "+er.file)
131    log.error("", ["Failed to parse package.json data."
132              ,"package.json must be actual JSON, not just JavaScript."
133              ,"","This is not a bug in npm."
134              ,"Tell the package author to fix their package.json file."
135              ].join("\n"), "JSON.parse")
136    break
137
138  case "E404":
139    er.code = "E404"
140    if (er.pkgid && er.pkgid !== "-") {
141      var msg = ["'"+er.pkgid+"' is not in the npm registry."
142                ,"You should bug the author to publish it"]
143      if (er.pkgid.match(/^node[\.\-]|[\.\-]js$/)) {
144        var s = er.pkgid.replace(/^node[\.\-]|[\.\-]js$/g, "")
145        if (s !== er.pkgid) {
146          s = s.replace(/[^a-z0-9]/g, ' ')
147          msg.push("\nMaybe try 'npm search " + s + "'")
148        }
149      }
150      msg.push("\nNote that you can also install from a"
151              ,"tarball, folder, or http url, or git url.")
152      log.error("404", msg.join("\n"))
153    }
154    break
155
156  case "EPUBLISHCONFLICT":
157    er.code = "EPUBLISHCONFLICT"
158    log.error("publish fail", ["Cannot publish over existing version."
159              ,"Bump the 'version' field, set the --force flag, or"
160              ,"    npm unpublish '"+er.pkgid+"'"
161              ,"and try again"
162              ].join("\n"))
163    break
164
165  case "EISGIT":
166    er.code = "EISGIT"
167    log.error("git", [er.message
168              ,"    "+er.path
169              ,"Refusing to remove it. Update manually,"
170              ,"or move it out of the way first."
171              ].join("\n"))
172    break
173
174  case "ECYCLE":
175    er.code = "ECYCLE"
176    log.error("cycle", [er.message
177              ,"While installing: "+er.pkgid
178              ,"Found a pathological dependency case that npm cannot solve."
179              ,"Please report this to the package author."
180              ].join("\n"))
181    break
182
183  case "EBADPLATFORM":
184    er.code = "EBADPLATFORM"
185    log.error("notsup", [er.message
186              ,"Not compatible with your operating system or architecture: "+er.pkgid
187              ,"Valid OS:    "+er.os.join(",")
188              ,"Valid Arch:  "+er.cpu.join(",")
189              ,"Actual OS:   "+process.platform
190              ,"Actual Arch: "+process.arch
191              ].join("\n"))
192    break
193
194  case "EEXIST":
195    log.error([er.message
196              ,"File exists: "+er.path
197              ,"Move it away, and try again."].join("\n"))
198    break
199
200  case "ENEEDAUTH":
201    log.error("need auth", [er.message
202              ,"You need to authorize this machine using `npm adduser`"
203              ].join("\n"))
204    break
205
206  case "ENOTSUP":
207    if (er.required) {
208      log.error("notsup", [er.message
209                ,"Not compatible with your version of node/npm: "+er.pkgid
210                ,"Required: "+JSON.stringify(er.required)
211                ,"Actual:   "
212                +JSON.stringify({npm:npm.version
213                                ,node:npm.config.get("node-version")})
214                ].join("\n"))
215      break
216    } // else passthrough
217
218  default:
219    log.error("", er.stack || er.message || er)
220    log.error("", ["If you need help, you may report this log at:"
221                  ,"    <http://github.com/isaacs/npm/issues>"
222                  ,"or email it to:"
223                  ,"    <npm-@googlegroups.com>"
224                  ].join("\n"))
225    printStack = false
226    break
227  }
228
229  var os = require("os")
230  // just a line break
231  console.error("")
232  log.error("System", os.type() + " " + os.release())
233  log.error("command", process.argv
234            .map(JSON.stringify).join(" "))
235  log.error("cwd", process.cwd())
236  log.error("node -v", process.version)
237  log.error("npm -v", npm.version)
238
239  ; [ "file"
240    , "path"
241    , "type"
242    , "syscall"
243    , "fstream_path"
244    , "fstream_unc_path"
245    , "fstream_type"
246    , "fstream_class"
247    , "fstream_finish_call"
248    , "fstream_linkpath"
249    , "code"
250    , "errno"
251    , "stack"
252    , "fstream_stack"
253    ].forEach(function (k) {
254      var v = er[k]
255      if (k === "stack") {
256        if (!printStack) return
257        if (!v) v = er.message
258      }
259      if (!v) return
260      if (k === "fstream_stack") v = v.join("\n")
261      log.error(k, v)
262    })
263
264  exit(typeof er.errno === "number" ? er.errno : 1)
265}
266
267var writingLogFile = false
268function writeLogFile (cb) {
269  if (writingLogFile) return cb()
270  writingLogFile = true
271  wroteLogFile = true
272
273  var fs = require("graceful-fs")
274    , fstr = fs.createWriteStream("npm-debug.log")
275    , util = require("util")
276    , eol = process.platform === "win32" ? "\r\n" : "\n"
277    , out = ""
278
279  log.record.forEach(function (m) {
280    var pref = [m.id, m.level]
281    if (m.prefix) pref.push(m.prefix)
282    pref = pref.join(' ')
283
284    m.message.trim().split(/\r?\n/).map(function (line) {
285      return (pref + ' ' + line).trim()
286    }).forEach(function (line) {
287      out += line + eol
288    })
289  })
290
291  fstr.end(out)
292  fstr.on("close", cb)
293}