PageRenderTime 31ms CodeModel.GetById 11ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/jitsu/node_modules/npm/lib/utils/error-handler.js

https://github.com/yyf/dmv
JavaScript | 264 lines | 230 code | 29 blank | 5 comment | 35 complexity | f5548ebbc53dde6207b834e985f8e432 MD5 | raw file
  1
  2module.exports = errorHandler
  3
  4var cbCalled = false
  5  , log = require("./log.js")
  6  , npm = require("../npm.js")
  7  , rm = require("rimraf")
  8  , constants = require("constants")
  9  , itWorked = false
 10  , path = require("path")
 11  , ini = require("./ini.js")
 12  , wroteLogFile = false
 13
 14
 15process.on("exit", function (code) {
 16  // console.error("exit", code)
 17  if (!ini.resolved) return
 18  if (code) itWorked = false
 19  if (itWorked) log("ok")
 20  else {
 21    if (!cbCalled) {
 22      log.error("cb() never called!\n ")
 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.win("not ok")
 32  }
 33  itWorked = false // ready for next exit
 34})
 35
 36function errorHandler (er) {
 37  // console.error("errorHandler", er)
 38  if (!ini.resolved) {
 39    // logging won't work unless we pretend that it's ready
 40    er = er || new Error("Exit prior to config file resolving.")
 41    console.error(er.stack || er.message)
 42  }
 43
 44  if (cbCalled) {
 45    er = er || new Error("Callback called more than once.")
 46  }
 47
 48  cbCalled = true
 49  if (!er) return exit(0)
 50  if (!(er instanceof Error)) {
 51    log.error(er)
 52    return exit(1, true)
 53  }
 54
 55  var m = er.code || er.message.match(/^(?:Error: )?(E[A-Z]+)/)
 56  if (m) {
 57    m = m[1]
 58    if (!constants[m] && !npm[m]) constants[m] = {}
 59    er.errno = npm[m] || constants[m]
 60  }
 61
 62  switch (er.errno) {
 63  case "ECONNREFUSED":
 64  case constants.ECONNREFUSED:
 65    log.error(er)
 66    log.error(["If you are using Cygwin, please set up your /etc/resolv.conf"
 67              ,"See step 4 in this wiki page:"
 68              ,"    http://github.com/ry/node/wiki/Building-node.js-on-Cygwin-%28Windows%29"
 69              ,"If you are not using Cygwin, please report this"
 70              ,"at <http://github.com/isaacs/npm/issues>"
 71              ,"or email it to <npm-@googlegroups.com>"
 72              ].join("\n"))
 73    break
 74
 75  case "EACCES":
 76  case "EPERM":
 77  case constants.EACCES:
 78  case constants.EPERM:
 79    log.error(er)
 80    log.error(["",
 81              "Please use 'sudo' or log in as root to run this command."
 82              ,""
 83              ,"    sudo npm "
 84                +npm.config.get("argv").original.map(JSON.stringify).join(" ")
 85              ,""
 86              ,"or set the 'unsafe-perm' config var to true."
 87              ,""
 88              ,"    npm config set unsafe-perm true"
 89              ].join("\n"))
 90    break
 91
 92  case npm.ELIFECYCLE:
 93    er.code = "ELIFECYCLE"
 94    log.error(er.message)
 95    log.error(["","Failed at the "+er.pkgid+" "+er.stage+" script."
 96              ,"This is most likely a problem with the "+er.pkgname+" package,"
 97              ,"not with npm itself."
 98              ,"Tell the author that this fails on your system:"
 99              ,"    "+er.script
100              ,"You can get their info via:"
101              ,"    npm owner ls "+er.pkgname
102              ,"There is likely additional logging output above."
103              ].join("\n"))
104    break
105
106  case npm.EJSONPARSE:
107    er.code = "EJSONPARSE"
108    log.error(er.message)
109    log.error("File: "+er.file)
110    log.error(["Failed to parse package.json data."
111              ,"package.json must be actual JSON, not just JavaScript."
112              ,"","This is not a bug in npm."
113              ,"Tell the package author to fix their package.json file."
114              ].join("\n"), "JSON.parse")
115    break
116
117  case npm.E404:
118    er.code = "E404"
119    if (er.pkgid && er.pkgid !== "-") {
120      var msg = ["'"+er.pkgid+"' is not in the npm registry."
121                ,"You could maybe bug the author to publish it"]
122      if (er.pkgid.match(/^node[\.\-]|[\.\-]js$/)) {
123        var s = er.pkgid.replace(/^node[\.\-]|[\.\-]js$/g, "")
124        if (s !== er.pkgid) {
125          s = s.replace(/[^a-z0-9]/g, ' ')
126          msg.push("Maybe try 'npm search " + s + "'")
127        }
128      }
129      msg.push("Note that you can also install from a tarball or folder.")
130      log.error(msg.join("\n"), "404")
131    }
132    break
133
134  case npm.EPUBLISHCONFLICT:
135    er.code = "EPUBLISHCONFLICT"
136    log.error(["Cannot publish over existing version."
137              ,"Bump the 'version' field, set the --force flag, or"
138              ,"    npm unpublish '"+er.pkgid+"'"
139              ,"and try again"
140              ].join("\n"), "publish fail" )
141    break
142
143  case npm.EISGIT:
144    er.code = "EISGIT"
145    log.error([er.message
146              ,"    "+er.path
147              ,"Refusing to remove it. Update manually,"
148              ,"or move it out of the way first."
149              ].join("\n"), "git" )
150    break
151
152  case npm.ECYCLE:
153    er.code = "ECYCLE"
154    log.error([er.message
155              ,"While installing: "+er.pkgid
156              ,"Found a pathological dependency case that npm cannot solve."
157              ,"Please report this to the package author."
158              ].join("\n"))
159    break
160
161  case npm.ENOTSUP:
162    er.code = "ENOTSUP"
163    log.error([er.message
164              ,"Not compatible with your version of node/npm: "+er.pkgid
165              ,"Required: "+JSON.stringify(er.required)
166              ,"Actual:   "
167              +JSON.stringify({npm:npm.version
168                              ,node:npm.config.get("node-version")})
169              ].join("\n"))
170    break
171
172  case "EEXIST":
173  case constants.EEXIST:
174    log.error([er.message
175              ,"File exists: "+er.path
176              ,"Move it away, and try again."].join("\n"))
177    break
178
179  default:
180    log.error(er)
181    log.error(["Report this *entire* log at:"
182              ,"    <http://github.com/isaacs/npm/issues>"
183              ,"or email it to:"
184              ,"    <npm-@googlegroups.com>"
185              ].join("\n"))
186    break
187  }
188
189  var os = require("os")
190  log.error("")
191  log.error(os.type() + " " + os.release(), "System")
192  log.error(process.argv
193            .map(JSON.stringify).join(" "), "command")
194  log.error(process.cwd(), "cwd")
195  log.error(process.version, "node -v")
196  log.error(npm.version, "npm -v")
197
198  ; [ "file"
199    , "path"
200    , "type"
201    , "syscall"
202    , "fstream_path"
203    , "fstream_unc_path"
204    , "fstream_type"
205    , "fstream_class"
206    , "fstream_finish_call"
207    , "fstream_linkpath"
208    , "arguments"
209    , "code"
210    , "message"
211    ].forEach(function (k) {
212      if (er[k]) log.error(er[k], k)
213    })
214
215  if (er.fstream_stack) {
216    log.error(er.fstream_stack.join("\n"), "fstream_stack")
217  }
218
219  if (er.errno && typeof er.errno !== "object") log.error(er.errno, "errno")
220  exit(typeof er.errno === "number" ? er.errno : 1)
221}
222
223function exit (code, noLog) {
224  var doExit = npm.config.get("_exit")
225  log.verbose([code, doExit], "exit")
226  if (log.level === log.LEVEL.silent) noLog = true
227
228  if (code && !noLog) writeLogFile(reallyExit)
229  else rm("npm-debug.log", function () { rm(npm.tmp, reallyExit) })
230
231  function reallyExit() {
232    itWorked = !code
233    //if (!itWorked) {
234      if (!doExit) process.emit("exit", code)
235      else process.exit(code)
236    //}
237  }
238}
239
240var writingLogFile = false
241function writeLogFile (cb) {
242  if (writingLogFile) return cb()
243  writingLogFile = true
244  wroteLogFile = true
245
246  var fs = require("graceful-fs")
247    , fstr = fs.createWriteStream("npm-debug.log")
248    , util = require("util")
249
250  log.history.forEach(function (m) {
251    var lvl = log.LEVEL[m.level]
252      , pref = m.pref ? " " + m.pref : ""
253      , b = lvl + pref + " "
254      , eol = process.platform === "win32" ? "\r\n" : "\n"
255      , msg = typeof m.msg === "string" ? m.msg
256            : msg instanceof Error ? msg.stack || msg.message
257            : util.inspect(m.msg, 0, 4)
258    fstr.write(new Buffer(b
259                         +(msg.split(/\r?\n+/).join(eol+b))
260                         + eol))
261  })
262  fstr.end()
263  fstr.on("close", cb)
264}