PageRenderTime 62ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/mosaic/mosaic-distribution-dependencies
JavaScript | 305 lines | 257 code | 39 blank | 9 comment | 44 complexity | 708c5c85e838c2cfa4a63d21ad68b794 MD5 | raw file
Possible License(s): 0BSD, MIT, WTFPL, Apache-2.0, BSD-2-Clause, LGPL-2.1, BSD-3-Clause, MPL-2.0-no-copyleft-exception
  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 "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. case "E404":
  117. er.code = "E404"
  118. if (er.pkgid && er.pkgid !== "-") {
  119. var msg = ["'"+er.pkgid+"' is not in the npm registry."
  120. ,"You should bug the author to publish it"]
  121. if (er.pkgid.match(/^node[\.\-]|[\.\-]js$/)) {
  122. var s = er.pkgid.replace(/^node[\.\-]|[\.\-]js$/g, "")
  123. if (s !== er.pkgid) {
  124. s = s.replace(/[^a-z0-9]/g, ' ')
  125. msg.push("\nMaybe try 'npm search " + s + "'")
  126. }
  127. }
  128. msg.push("\nNote that you can also install from a"
  129. ,"tarball, folder, or http url, or git url.")
  130. log.error("404", msg.join("\n"))
  131. }
  132. break
  133. case "EPUBLISHCONFLICT":
  134. er.code = "EPUBLISHCONFLICT"
  135. log.error("publish fail", ["Cannot publish over existing version."
  136. ,"Update the 'version' field in package.json and try again."
  137. ,""
  138. ,"If the previous version was published in error, see:"
  139. ," npm help unpublish"
  140. ,""
  141. ,"To automatically increment version numbers, see:"
  142. ," npm help version"
  143. ].join("\n"))
  144. break
  145. case "EISGIT":
  146. er.code = "EISGIT"
  147. log.error("git", [er.message
  148. ," "+er.path
  149. ,"Refusing to remove it. Update manually,"
  150. ,"or move it out of the way first."
  151. ].join("\n"))
  152. break
  153. case "ECYCLE":
  154. er.code = "ECYCLE"
  155. log.error("cycle", [er.message
  156. ,"While installing: "+er.pkgid
  157. ,"Found a pathological dependency case that npm cannot solve."
  158. ,"Please report this to the package author."
  159. ].join("\n"))
  160. break
  161. case "EBADPLATFORM":
  162. er.code = "EBADPLATFORM"
  163. log.error("notsup", [er.message
  164. ,"Not compatible with your operating system or architecture: "+er.pkgid
  165. ,"Valid OS: "+er.os.join(",")
  166. ,"Valid Arch: "+er.cpu.join(",")
  167. ,"Actual OS: "+process.platform
  168. ,"Actual Arch: "+process.arch
  169. ].join("\n"))
  170. break
  171. case "EEXIST":
  172. log.error([er.message
  173. ,"File exists: "+er.path
  174. ,"Move it away, and try again."].join("\n"))
  175. break
  176. case "ENEEDAUTH":
  177. log.error("need auth", [er.message
  178. ,"You need to authorize this machine using `npm adduser`"
  179. ].join("\n"))
  180. break
  181. case "EPEERINVALID":
  182. var peerErrors = Object.keys(er.peersDepending).map(function (peer) {
  183. return "Peer " + peer + " wants " + er.packageName + "@"
  184. + er.peersDepending[peer]
  185. })
  186. log.error("peerinvalid", [er.message].concat(peerErrors).join("\n"))
  187. break
  188. case "ENOTSUP":
  189. if (er.required) {
  190. log.error("notsup", [er.message
  191. ,"Not compatible with your version of node/npm: "+er.pkgid
  192. ,"Required: "+JSON.stringify(er.required)
  193. ,"Actual: "
  194. +JSON.stringify({npm:npm.version
  195. ,node:npm.config.get("node-version")})
  196. ].join("\n"))
  197. break
  198. } // else passthrough
  199. default:
  200. log.error("", er.stack || er.message || er)
  201. log.error("", ["If you need help, you may report this log at:"
  202. ," <http://github.com/isaacs/npm/issues>"
  203. ,"or email it to:"
  204. ," <npm-@googlegroups.com>"
  205. ].join("\n"))
  206. printStack = false
  207. break
  208. }
  209. var os = require("os")
  210. // just a line break
  211. console.error("")
  212. log.error("System", os.type() + " " + os.release())
  213. log.error("command", process.argv
  214. .map(JSON.stringify).join(" "))
  215. log.error("cwd", process.cwd())
  216. log.error("node -v", process.version)
  217. log.error("npm -v", npm.version)
  218. ; [ "file"
  219. , "path"
  220. , "type"
  221. , "syscall"
  222. , "fstream_path"
  223. , "fstream_unc_path"
  224. , "fstream_type"
  225. , "fstream_class"
  226. , "fstream_finish_call"
  227. , "fstream_linkpath"
  228. , "code"
  229. , "errno"
  230. , "stack"
  231. , "fstream_stack"
  232. ].forEach(function (k) {
  233. var v = er[k]
  234. if (k === "stack") {
  235. if (!printStack) return
  236. if (!v) v = er.message
  237. }
  238. if (!v) return
  239. if (k === "fstream_stack") v = v.join("\n")
  240. log.error(k, v)
  241. })
  242. exit(typeof er.errno === "number" ? er.errno : 1)
  243. }
  244. var writingLogFile = false
  245. function writeLogFile (cb) {
  246. if (writingLogFile) return cb()
  247. writingLogFile = true
  248. wroteLogFile = true
  249. var fs = require("graceful-fs")
  250. , fstr = fs.createWriteStream("npm-debug.log")
  251. , util = require("util")
  252. , eol = process.platform === "win32" ? "\r\n" : "\n"
  253. , out = ""
  254. log.record.forEach(function (m) {
  255. var pref = [m.id, m.level]
  256. if (m.prefix) pref.push(m.prefix)
  257. pref = pref.join(' ')
  258. m.message.trim().split(/\r?\n/).map(function (line) {
  259. return (pref + ' ' + line).trim()
  260. }).forEach(function (line) {
  261. out += line + eol
  262. })
  263. })
  264. fstr.end(out)
  265. fstr.on("close", cb)
  266. }