PageRenderTime 780ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/utils/error-handler.js

https://github.com/saywow/npm
JavaScript | 324 lines | 274 code | 41 blank | 9 comment | 44 complexity | 1c162a55cfe911bee79c818077ccfffd MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, WTFPL
  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 || 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.pkgid.match(/^node[\.\-]|[\.\-]js$/)) {
  130. var s = er.pkgid.replace(/^node[\.\-]|[\.\-]js$/g, "")
  131. if (s !== er.pkgid) {
  132. s = s.replace(/[^a-z0-9]/g, ' ')
  133. msg.push("\nMaybe try 'npm search " + s + "'")
  134. }
  135. }
  136. msg.push("\nNote that you can also install from a"
  137. ,"tarball, folder, or http url, or git url.")
  138. log.error("404", msg.join("\n"))
  139. }
  140. break
  141. case "EPUBLISHCONFLICT":
  142. er.code = "EPUBLISHCONFLICT"
  143. log.error("publish fail", ["Cannot publish over existing version."
  144. ,"Update the 'version' field in package.json and try again."
  145. ,""
  146. ,"If the previous version was published in error, see:"
  147. ," npm help unpublish"
  148. ,""
  149. ,"To automatically increment version numbers, see:"
  150. ," npm help version"
  151. ].join("\n"))
  152. break
  153. case "EISGIT":
  154. er.code = "EISGIT"
  155. log.error("git", [er.message
  156. ," "+er.path
  157. ,"Refusing to remove it. Update manually,"
  158. ,"or move it out of the way first."
  159. ].join("\n"))
  160. break
  161. case "ECYCLE":
  162. er.code = "ECYCLE"
  163. log.error("cycle", [er.message
  164. ,"While installing: "+er.pkgid
  165. ,"Found a pathological dependency case that npm cannot solve."
  166. ,"Please report this to the package author."
  167. ].join("\n"))
  168. break
  169. case "EBADPLATFORM":
  170. er.code = "EBADPLATFORM"
  171. log.error("notsup", [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. log.error([er.message
  181. ,"File exists: "+er.path
  182. ,"Move it away, and try again."].join("\n"))
  183. break
  184. case "ENEEDAUTH":
  185. log.error("need auth", [er.message
  186. ,"You need to authorize this machine using `npm adduser`"
  187. ].join("\n"))
  188. break
  189. case "EPEERINVALID":
  190. var peerErrors = Object.keys(er.peersDepending).map(function (peer) {
  191. return "Peer " + peer + " wants " + er.packageName + "@"
  192. + er.peersDepending[peer]
  193. })
  194. log.error("peerinvalid", [er.message].concat(peerErrors).join("\n"))
  195. break
  196. case "ENOTFOUND":
  197. log.error("network", [er.message
  198. ,"This is most likely not a problem with npm itself"
  199. ,"and is related to network connectivity."
  200. ,"In most cases you are behind a proxy or have bad network settings."
  201. ,"\nIf you are behind a proxy, please make sure that the"
  202. ,"'proxy' config is set properly. See: 'npm help config'"
  203. ].join("\n"))
  204. break
  205. case "ENOTSUP":
  206. if (er.required) {
  207. log.error("notsup", [er.message
  208. ,"Not compatible with your version of node/npm: "+er.pkgid
  209. ,"Required: "+JSON.stringify(er.required)
  210. ,"Actual: "
  211. +JSON.stringify({npm:npm.version
  212. ,node:npm.config.get("node-version")})
  213. ].join("\n"))
  214. break
  215. } // else passthrough
  216. default:
  217. log.error("", er.stack || er.message || er)
  218. log.error("", ["If you need help, you may report this log at:"
  219. ," <http://github.com/isaacs/npm/issues>"
  220. ,"or email it to:"
  221. ," <npm-@googlegroups.com>"
  222. ].join("\n"))
  223. printStack = false
  224. break
  225. }
  226. var os = require("os")
  227. // just a line break
  228. console.error("")
  229. log.error("System", os.type() + " " + os.release())
  230. log.error("command", process.argv
  231. .map(JSON.stringify).join(" "))
  232. log.error("cwd", process.cwd())
  233. log.error("node -v", process.version)
  234. log.error("npm -v", npm.version)
  235. ; [ "file"
  236. , "path"
  237. , "type"
  238. , "syscall"
  239. , "fstream_path"
  240. , "fstream_unc_path"
  241. , "fstream_type"
  242. , "fstream_class"
  243. , "fstream_finish_call"
  244. , "fstream_linkpath"
  245. , "code"
  246. , "errno"
  247. , "stack"
  248. , "fstream_stack"
  249. ].forEach(function (k) {
  250. var v = er[k]
  251. if (k === "stack") {
  252. if (!printStack) return
  253. if (!v) v = er.message
  254. }
  255. if (!v) return
  256. if (k === "fstream_stack") v = v.join("\n")
  257. log.error(k, v)
  258. })
  259. exit(typeof er.errno === "number" ? er.errno : 1)
  260. }
  261. var writingLogFile = false
  262. function writeLogFile (cb) {
  263. if (writingLogFile) return cb()
  264. writingLogFile = true
  265. wroteLogFile = true
  266. var fs = require("graceful-fs")
  267. , fstr = fs.createWriteStream("npm-debug.log")
  268. , util = require("util")
  269. , os = require("os")
  270. , out = ""
  271. log.record.forEach(function (m) {
  272. var pref = [m.id, m.level]
  273. if (m.prefix) pref.push(m.prefix)
  274. pref = pref.join(' ')
  275. m.message.trim().split(/\r?\n/).map(function (line) {
  276. return (pref + ' ' + line).trim()
  277. }).forEach(function (line) {
  278. out += line + os.EOL
  279. })
  280. })
  281. fstr.end(out)
  282. fstr.on("close", cb)
  283. }