PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/deps/npm/lib/utils/error-handler.js

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