PageRenderTime 28ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/node-v0.8.11/deps/npm/lib/utils/error-handler.js

https://bitbucket.org/jmchen/pysource
JavaScript | 293 lines | 246 code | 38 blank | 9 comment | 44 complexity | e7e1e7e8d0e7dc12431667c0fa6ed612 MD5 | raw file
Possible License(s): BSD-2-Clause, 0BSD, MIT, WTFPL, BSD-3-Clause, Apache-2.0
  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. ,"Bump the 'version' field, set the --force flag, or"
  137. ," npm unpublish '"+er.pkgid+"'"
  138. ,"and try again"
  139. ].join("\n"))
  140. break
  141. case "EISGIT":
  142. er.code = "EISGIT"
  143. log.error("git", [er.message
  144. ," "+er.path
  145. ,"Refusing to remove it. Update manually,"
  146. ,"or move it out of the way first."
  147. ].join("\n"))
  148. break
  149. case "ECYCLE":
  150. er.code = "ECYCLE"
  151. log.error("cycle", [er.message
  152. ,"While installing: "+er.pkgid
  153. ,"Found a pathological dependency case that npm cannot solve."
  154. ,"Please report this to the package author."
  155. ].join("\n"))
  156. break
  157. case "EBADPLATFORM":
  158. er.code = "EBADPLATFORM"
  159. log.error("notsup", [er.message
  160. ,"Not compatible with your operating system or architecture: "+er.pkgid
  161. ,"Valid OS: "+er.os.join(",")
  162. ,"Valid Arch: "+er.cpu.join(",")
  163. ,"Actual OS: "+process.platform
  164. ,"Actual Arch: "+process.arch
  165. ].join("\n"))
  166. break
  167. case "EEXIST":
  168. log.error([er.message
  169. ,"File exists: "+er.path
  170. ,"Move it away, and try again."].join("\n"))
  171. break
  172. case "ENEEDAUTH":
  173. log.error("need auth", [er.message
  174. ,"You need to authorize this machine using `npm adduser`"
  175. ].join("\n"))
  176. break
  177. case "ENOTSUP":
  178. if (er.required) {
  179. log.error("notsup", [er.message
  180. ,"Not compatible with your version of node/npm: "+er.pkgid
  181. ,"Required: "+JSON.stringify(er.required)
  182. ,"Actual: "
  183. +JSON.stringify({npm:npm.version
  184. ,node:npm.config.get("node-version")})
  185. ].join("\n"))
  186. break
  187. } // else passthrough
  188. default:
  189. log.error("", er.stack || er.message || er)
  190. log.error("", ["If you need help, you may report this log at:"
  191. ," <http://github.com/isaacs/npm/issues>"
  192. ,"or email it to:"
  193. ," <npm-@googlegroups.com>"
  194. ].join("\n"))
  195. printStack = false
  196. break
  197. }
  198. var os = require("os")
  199. // just a line break
  200. console.error("")
  201. log.error("System", os.type() + " " + os.release())
  202. log.error("command", process.argv
  203. .map(JSON.stringify).join(" "))
  204. log.error("cwd", process.cwd())
  205. log.error("node -v", process.version)
  206. log.error("npm -v", npm.version)
  207. ; [ "file"
  208. , "path"
  209. , "type"
  210. , "syscall"
  211. , "fstream_path"
  212. , "fstream_unc_path"
  213. , "fstream_type"
  214. , "fstream_class"
  215. , "fstream_finish_call"
  216. , "fstream_linkpath"
  217. , "code"
  218. , "errno"
  219. , "stack"
  220. , "fstream_stack"
  221. ].forEach(function (k) {
  222. var v = er[k]
  223. if (k === "stack") {
  224. if (!printStack) return
  225. if (!v) v = er.message
  226. }
  227. if (!v) return
  228. if (k === "fstream_stack") v = v.join("\n")
  229. log.error(k, v)
  230. })
  231. exit(typeof er.errno === "number" ? er.errno : 1)
  232. }
  233. var writingLogFile = false
  234. function writeLogFile (cb) {
  235. if (writingLogFile) return cb()
  236. writingLogFile = true
  237. wroteLogFile = true
  238. var fs = require("graceful-fs")
  239. , fstr = fs.createWriteStream("npm-debug.log")
  240. , util = require("util")
  241. , eol = process.platform === "win32" ? "\r\n" : "\n"
  242. , out = ""
  243. log.record.forEach(function (m) {
  244. var pref = [m.id, m.level]
  245. if (m.prefix) pref.push(m.prefix)
  246. pref = pref.join(' ')
  247. m.message.trim().split(/\r?\n/).map(function (line) {
  248. return (pref + ' ' + line).trim()
  249. }).forEach(function (line) {
  250. out += line + eol
  251. })
  252. })
  253. fstr.end(out)
  254. fstr.on("close", cb)
  255. }