PageRenderTime 141ms CodeModel.GetById 16ms RepoModel.GetById 2ms app.codeStats 0ms

/node_modules/jitsu/node_modules/npm/lib/utils/error-handler.js

https://github.com/yyf/dmv
JavaScript | 264 lines | 230 code | 29 blank | 5 comment | 35 complexity | f5548ebbc53dde6207b834e985f8e432 MD5 | raw file
Possible License(s): MIT, Apache-2.0, GPL-2.0, BSD-3-Clause
  1. module.exports = errorHandler
  2. var cbCalled = false
  3. , log = require("./log.js")
  4. , npm = require("../npm.js")
  5. , rm = require("rimraf")
  6. , constants = require("constants")
  7. , itWorked = false
  8. , path = require("path")
  9. , ini = require("./ini.js")
  10. , wroteLogFile = false
  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("ok")
  16. else {
  17. if (!cbCalled) {
  18. log.error("cb() never called!\n ")
  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.win("not ok")
  28. }
  29. itWorked = false // ready for next exit
  30. })
  31. function errorHandler (er) {
  32. // console.error("errorHandler", er)
  33. if (!ini.resolved) {
  34. // logging won't work unless we pretend that it's ready
  35. er = er || new Error("Exit prior to config file resolving.")
  36. console.error(er.stack || er.message)
  37. }
  38. if (cbCalled) {
  39. er = er || new Error("Callback called more than once.")
  40. }
  41. cbCalled = true
  42. if (!er) return exit(0)
  43. if (!(er instanceof Error)) {
  44. log.error(er)
  45. return exit(1, true)
  46. }
  47. var m = er.code || er.message.match(/^(?:Error: )?(E[A-Z]+)/)
  48. if (m) {
  49. m = m[1]
  50. if (!constants[m] && !npm[m]) constants[m] = {}
  51. er.errno = npm[m] || constants[m]
  52. }
  53. switch (er.errno) {
  54. case "ECONNREFUSED":
  55. case constants.ECONNREFUSED:
  56. log.error(er)
  57. log.error(["If you are using Cygwin, please set up your /etc/resolv.conf"
  58. ,"See step 4 in this wiki page:"
  59. ," http://github.com/ry/node/wiki/Building-node.js-on-Cygwin-%28Windows%29"
  60. ,"If you are not using Cygwin, please report this"
  61. ,"at <http://github.com/isaacs/npm/issues>"
  62. ,"or email it to <npm-@googlegroups.com>"
  63. ].join("\n"))
  64. break
  65. case "EACCES":
  66. case "EPERM":
  67. case constants.EACCES:
  68. case constants.EPERM:
  69. log.error(er)
  70. log.error(["",
  71. "Please use 'sudo' or log in as root to run this command."
  72. ,""
  73. ," sudo npm "
  74. +npm.config.get("argv").original.map(JSON.stringify).join(" ")
  75. ,""
  76. ,"or set the 'unsafe-perm' config var to true."
  77. ,""
  78. ," npm config set unsafe-perm true"
  79. ].join("\n"))
  80. break
  81. case npm.ELIFECYCLE:
  82. er.code = "ELIFECYCLE"
  83. log.error(er.message)
  84. log.error(["","Failed at the "+er.pkgid+" "+er.stage+" script."
  85. ,"This is most likely a problem with the "+er.pkgname+" package,"
  86. ,"not with npm itself."
  87. ,"Tell the author that this fails on your system:"
  88. ," "+er.script
  89. ,"You can get their info via:"
  90. ," npm owner ls "+er.pkgname
  91. ,"There is likely additional logging output above."
  92. ].join("\n"))
  93. break
  94. case npm.EJSONPARSE:
  95. er.code = "EJSONPARSE"
  96. log.error(er.message)
  97. log.error("File: "+er.file)
  98. log.error(["Failed to parse package.json data."
  99. ,"package.json must be actual JSON, not just JavaScript."
  100. ,"","This is not a bug in npm."
  101. ,"Tell the package author to fix their package.json file."
  102. ].join("\n"), "JSON.parse")
  103. break
  104. case npm.E404:
  105. er.code = "E404"
  106. if (er.pkgid && er.pkgid !== "-") {
  107. var msg = ["'"+er.pkgid+"' is not in the npm registry."
  108. ,"You could maybe bug the author to publish it"]
  109. if (er.pkgid.match(/^node[\.\-]|[\.\-]js$/)) {
  110. var s = er.pkgid.replace(/^node[\.\-]|[\.\-]js$/g, "")
  111. if (s !== er.pkgid) {
  112. s = s.replace(/[^a-z0-9]/g, ' ')
  113. msg.push("Maybe try 'npm search " + s + "'")
  114. }
  115. }
  116. msg.push("Note that you can also install from a tarball or folder.")
  117. log.error(msg.join("\n"), "404")
  118. }
  119. break
  120. case npm.EPUBLISHCONFLICT:
  121. er.code = "EPUBLISHCONFLICT"
  122. log.error(["Cannot publish over existing version."
  123. ,"Bump the 'version' field, set the --force flag, or"
  124. ," npm unpublish '"+er.pkgid+"'"
  125. ,"and try again"
  126. ].join("\n"), "publish fail" )
  127. break
  128. case npm.EISGIT:
  129. er.code = "EISGIT"
  130. log.error([er.message
  131. ," "+er.path
  132. ,"Refusing to remove it. Update manually,"
  133. ,"or move it out of the way first."
  134. ].join("\n"), "git" )
  135. break
  136. case npm.ECYCLE:
  137. er.code = "ECYCLE"
  138. log.error([er.message
  139. ,"While installing: "+er.pkgid
  140. ,"Found a pathological dependency case that npm cannot solve."
  141. ,"Please report this to the package author."
  142. ].join("\n"))
  143. break
  144. case npm.ENOTSUP:
  145. er.code = "ENOTSUP"
  146. log.error([er.message
  147. ,"Not compatible with your version of node/npm: "+er.pkgid
  148. ,"Required: "+JSON.stringify(er.required)
  149. ,"Actual: "
  150. +JSON.stringify({npm:npm.version
  151. ,node:npm.config.get("node-version")})
  152. ].join("\n"))
  153. break
  154. case "EEXIST":
  155. case constants.EEXIST:
  156. log.error([er.message
  157. ,"File exists: "+er.path
  158. ,"Move it away, and try again."].join("\n"))
  159. break
  160. default:
  161. log.error(er)
  162. log.error(["Report this *entire* log at:"
  163. ," <http://github.com/isaacs/npm/issues>"
  164. ,"or email it to:"
  165. ," <npm-@googlegroups.com>"
  166. ].join("\n"))
  167. break
  168. }
  169. var os = require("os")
  170. log.error("")
  171. log.error(os.type() + " " + os.release(), "System")
  172. log.error(process.argv
  173. .map(JSON.stringify).join(" "), "command")
  174. log.error(process.cwd(), "cwd")
  175. log.error(process.version, "node -v")
  176. log.error(npm.version, "npm -v")
  177. ; [ "file"
  178. , "path"
  179. , "type"
  180. , "syscall"
  181. , "fstream_path"
  182. , "fstream_unc_path"
  183. , "fstream_type"
  184. , "fstream_class"
  185. , "fstream_finish_call"
  186. , "fstream_linkpath"
  187. , "arguments"
  188. , "code"
  189. , "message"
  190. ].forEach(function (k) {
  191. if (er[k]) log.error(er[k], k)
  192. })
  193. if (er.fstream_stack) {
  194. log.error(er.fstream_stack.join("\n"), "fstream_stack")
  195. }
  196. if (er.errno && typeof er.errno !== "object") log.error(er.errno, "errno")
  197. exit(typeof er.errno === "number" ? er.errno : 1)
  198. }
  199. function exit (code, noLog) {
  200. var doExit = npm.config.get("_exit")
  201. log.verbose([code, doExit], "exit")
  202. if (log.level === log.LEVEL.silent) noLog = true
  203. if (code && !noLog) writeLogFile(reallyExit)
  204. else rm("npm-debug.log", function () { rm(npm.tmp, reallyExit) })
  205. function reallyExit() {
  206. itWorked = !code
  207. //if (!itWorked) {
  208. if (!doExit) process.emit("exit", code)
  209. else process.exit(code)
  210. //}
  211. }
  212. }
  213. var writingLogFile = false
  214. function writeLogFile (cb) {
  215. if (writingLogFile) return cb()
  216. writingLogFile = true
  217. wroteLogFile = true
  218. var fs = require("graceful-fs")
  219. , fstr = fs.createWriteStream("npm-debug.log")
  220. , util = require("util")
  221. log.history.forEach(function (m) {
  222. var lvl = log.LEVEL[m.level]
  223. , pref = m.pref ? " " + m.pref : ""
  224. , b = lvl + pref + " "
  225. , eol = process.platform === "win32" ? "\r\n" : "\n"
  226. , msg = typeof m.msg === "string" ? m.msg
  227. : msg instanceof Error ? msg.stack || msg.message
  228. : util.inspect(m.msg, 0, 4)
  229. fstr.write(new Buffer(b
  230. +(msg.split(/\r?\n+/).join(eol+b))
  231. + eol))
  232. })
  233. fstr.end()
  234. fstr.on("close", cb)
  235. }