PageRenderTime 61ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/hook.io/node_modules/npm/lib/utils/error-handler.js

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