PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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