PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/Fenchurch/hybris-landscapr
JavaScript | 281 lines | 234 code | 38 blank | 9 comment | 41 complexity | 25413742f19484a6df168256b8dd33da MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, 0BSD, MIT, 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. , 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. // console.error("errorHandler", er)
  59. if (!ini.resolved) {
  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. break
  85. case "EACCES":
  86. case "EPERM":
  87. log.error("", er)
  88. log.error("", ["\nPlease try running this command again as root/Administrator."
  89. ].join("\n"))
  90. break
  91. case "ELIFECYCLE":
  92. er.code = "ELIFECYCLE"
  93. log.error("", er.message)
  94. log.error("", ["","Failed at the "+er.pkgid+" "+er.stage+" script."
  95. ,"This is most likely a problem with the "+er.pkgname+" package,"
  96. ,"not with npm itself."
  97. ,"Tell the author that this fails on your system:"
  98. ," "+er.script
  99. ,"You can get their info via:"
  100. ," npm owner ls "+er.pkgname
  101. ,"There is likely additional logging output above."
  102. ].join("\n"))
  103. break
  104. case "EJSONPARSE":
  105. er.code = "EJSONPARSE"
  106. log.error("", er.message)
  107. log.error("", "File: "+er.file)
  108. log.error("", ["Failed to parse package.json data."
  109. ,"package.json must be actual JSON, not just JavaScript."
  110. ,"","This is not a bug in npm."
  111. ,"Tell the package author to fix their package.json file."
  112. ].join("\n"), "JSON.parse")
  113. break
  114. case "E404":
  115. er.code = "E404"
  116. if (er.pkgid && er.pkgid !== "-") {
  117. var msg = ["'"+er.pkgid+"' is not in the npm registry."
  118. ,"You should bug the author to publish it"]
  119. if (er.pkgid.match(/^node[\.\-]|[\.\-]js$/)) {
  120. var s = er.pkgid.replace(/^node[\.\-]|[\.\-]js$/g, "")
  121. if (s !== er.pkgid) {
  122. s = s.replace(/[^a-z0-9]/g, ' ')
  123. msg.push("\nMaybe try 'npm search " + s + "'")
  124. }
  125. }
  126. msg.push("\nNote that you can also install from a"
  127. ,"tarball, folder, or http url, or git url.")
  128. log.error("404", msg.join("\n"))
  129. }
  130. break
  131. case "EPUBLISHCONFLICT":
  132. er.code = "EPUBLISHCONFLICT"
  133. log.error("publish fail", ["Cannot publish over existing version."
  134. ,"Bump the 'version' field, set the --force flag, or"
  135. ," npm unpublish '"+er.pkgid+"'"
  136. ,"and try again"
  137. ].join("\n"))
  138. break
  139. case "EISGIT":
  140. er.code = "EISGIT"
  141. log.error("git", [er.message
  142. ," "+er.path
  143. ,"Refusing to remove it. Update manually,"
  144. ,"or move it out of the way first."
  145. ].join("\n"))
  146. break
  147. case "ECYCLE":
  148. er.code = "ECYCLE"
  149. log.error("cycle", [er.message
  150. ,"While installing: "+er.pkgid
  151. ,"Found a pathological dependency case that npm cannot solve."
  152. ,"Please report this to the package author."
  153. ].join("\n"))
  154. break
  155. case "EBADPLATFORM":
  156. er.code = "EBADPLATFORM"
  157. log.error("notsup", [er.message
  158. ,"Not compatible with your operating system or architecture: "+er.pkgid
  159. ,"Valid OS: "+er.os.join(",")
  160. ,"Valid Arch: "+er.cpu.join(",")
  161. ,"Actual OS: "+process.platform
  162. ,"Actual Arch: "+process.arch
  163. ].join("\n"))
  164. break
  165. case "EEXIST":
  166. log.error([er.message
  167. ,"File exists: "+er.path
  168. ,"Move it away, and try again."].join("\n"))
  169. break
  170. case "ENOTSUP":
  171. if (er.required) {
  172. log.error("notsup", [er.message
  173. ,"Not compatible with your version of node/npm: "+er.pkgid
  174. ,"Required: "+JSON.stringify(er.required)
  175. ,"Actual: "
  176. +JSON.stringify({npm:npm.version
  177. ,node:npm.config.get("node-version")})
  178. ].join("\n"))
  179. break
  180. } // else passthrough
  181. default:
  182. log.error("", er)
  183. log.error("", ["You may report this log at:"
  184. ," <http://github.com/isaacs/npm/issues>"
  185. ,"or email it to:"
  186. ," <npm-@googlegroups.com>"
  187. ].join("\n"))
  188. break
  189. }
  190. var os = require("os")
  191. // just a line break
  192. console.error("")
  193. log.error("System", os.type() + " " + os.release())
  194. log.error("command", process.argv
  195. .map(JSON.stringify).join(" "))
  196. log.error("cwd", process.cwd())
  197. log.error("node -v", process.version)
  198. log.error("npm -v", npm.version)
  199. ; [ "file"
  200. , "path"
  201. , "type"
  202. , "syscall"
  203. , "fstream_path"
  204. , "fstream_unc_path"
  205. , "fstream_type"
  206. , "fstream_class"
  207. , "fstream_finish_call"
  208. , "fstream_linkpath"
  209. , "code"
  210. , "message"
  211. , "errno"
  212. ].forEach(function (k) {
  213. if (er[k]) log.error(k, er[k])
  214. })
  215. if (er.fstream_stack) {
  216. log.error("fstream_stack", er.fstream_stack.join("\n"))
  217. }
  218. if (er.errno && typeof er.errno !== "object") log.error(er.errno, "errno")
  219. exit(typeof er.errno === "number" ? er.errno : 1)
  220. }
  221. var writingLogFile = false
  222. function writeLogFile (cb) {
  223. if (writingLogFile) return cb()
  224. writingLogFile = true
  225. wroteLogFile = true
  226. var fs = require("graceful-fs")
  227. , fstr = fs.createWriteStream("npm-debug.log")
  228. , util = require("util")
  229. , eol = process.platform === "win32" ? "\r\n" : "\n"
  230. , out = ""
  231. log.record.forEach(function (m) {
  232. var pref = [m.id, m.level]
  233. if (m.prefix) pref.push(m.prefix)
  234. pref = pref.join(' ')
  235. m.message.trim().split(/\r?\n/).map(function (line) {
  236. return (pref + ' ' + line).trim()
  237. }).forEach(function (line) {
  238. out += line + eol
  239. })
  240. })
  241. fstr.end(out)
  242. fstr.on("close", cb)
  243. }