PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/utils/error-handler.js

https://github.com/jmoyers/npm
JavaScript | 211 lines | 181 code | 25 blank | 5 comment | 23 complexity | 78d3ecaed2c09450a5a53c75d0cc6c4a MD5 | raw file
  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. process.on("exit", function (code) {
  11. // console.error("exit", code)
  12. if (!ini.resolved) return
  13. if (code) itWorked = false
  14. if (itWorked) log("ok")
  15. else {
  16. if (!cbCalled) {
  17. log.error("cb() never called!\n ")
  18. }
  19. log.error([""
  20. ,"Additional logging details can be found in:"
  21. ," " + path.resolve("npm-debug.log")
  22. ].join("\n"))
  23. log.win("not ok")
  24. }
  25. itWorked = false // ready for next exit
  26. })
  27. function errorHandler (er) {
  28. // console.error("errorHandler", er)
  29. if (!ini.resolved) {
  30. // logging won't work unless we pretend that it's ready
  31. er = er || new Error("Exit prior to config file resolving.")
  32. console.error(er.stack || er.message)
  33. }
  34. if (cbCalled) {
  35. er = er || new Error("Callback called more than once.")
  36. }
  37. cbCalled = true
  38. if (!er) return exit(0)
  39. if (!(er instanceof Error)) {
  40. log.error(er)
  41. return exit(1)
  42. }
  43. var m = er.code || er.message.match(/^(?:Error: )?(E[A-Z]+)/)
  44. if (m) {
  45. m = m[1]
  46. if (!constants[m] && !npm[m]) constants[m] = {}
  47. er.errno = npm[m] || constants[m]
  48. }
  49. switch (er.errno) {
  50. case constants.ECONNREFUSED:
  51. log.error(er)
  52. log.error(["If you are using Cygwin, please set up your /etc/resolv.conf"
  53. ,"See step 4 in this wiki page:"
  54. ," http://github.com/ry/node/wiki/Building-node.js-on-Cygwin-%28Windows%29"
  55. ,"If you are not using Cygwin, please report this"
  56. ,"at <http://github.com/isaacs/npm/issues>"
  57. ,"or email it to <npm-@googlegroups.com>"
  58. ].join("\n"))
  59. break
  60. case constants.EACCES:
  61. case constants.EPERM:
  62. log.error(er)
  63. log.error(["",
  64. "Please use 'sudo' or log in as root to run this command."
  65. ,""
  66. ," sudo npm "
  67. +npm.config.get("argv").original.map(JSON.stringify).join(" ")
  68. ,""
  69. ,"or set the 'unsafe-perm' config var to true."
  70. ,""
  71. ," npm config set unsafe-perm true"
  72. ].join("\n"))
  73. break
  74. case npm.ELIFECYCLE:
  75. log.error(er.message)
  76. log.error(["","Failed at the "+er.pkgid+" "+er.stage+" script."
  77. ,"This is most likely a problem with the "+er.pkgname+" package,"
  78. ,"not with npm itself."
  79. ,"Tell the author that this fails on your system:"
  80. ," "+er.script
  81. ,"You can get their info via:"
  82. ," npm owner ls "+er.pkgname
  83. ,"There is likely additional logging output above."
  84. ].join("\n"))
  85. break
  86. case npm.EJSONPARSE:
  87. log.error(er.message)
  88. log.error("File: "+er.file)
  89. log.error(["Failed to parse package.json data."
  90. ,"package.json must be actual JSON, not just JavaScript."
  91. ,"","This is not a bug in npm."
  92. ,"Tell the package author to fix their package.json file."
  93. ].join("\n"), "JSON.parse")
  94. break
  95. case npm.E404:
  96. log.error(["'"+er.pkgid+"' is not in the npm registry."
  97. ,"You should bug the author to publish it."
  98. ,"Note that you can also install from a tarball or folder."
  99. ].join("\n"), "404")
  100. break
  101. case npm.EPUBLISHCONFLICT:
  102. log.error(["Cannot publish over existing version."
  103. ,"Bump the 'version' field, set the --force flag, or"
  104. ," npm unpublish '"+er.pkgid+"'"
  105. ,"and try again"
  106. ].join("\n"), "publish fail" )
  107. break
  108. case npm.EISGIT:
  109. log.error([er.message
  110. ," "+er.path
  111. ,"Refusing to remove it. Update manually,"
  112. ,"or move it out of the way first."
  113. ].join("\n"), "git" )
  114. break
  115. case npm.ECYCLE:
  116. log.error([er.message
  117. ,"While installing: "+er.pkgid
  118. ,"Found a pathological dependency case that npm cannot solve."
  119. ,"Please report this to the package author."
  120. ].join("\n"))
  121. break
  122. case npm.EENGINE:
  123. log.error([er.message
  124. ,"Not compatible with your version of node/npm: "+er.pkgid
  125. ,"Required: "+JSON.stringify(er.required)
  126. ,"Actual: "
  127. +JSON.stringify({npm:npm.version
  128. ,node:npm.config.get("node-version")})
  129. ].join("\n"))
  130. break
  131. case constants.EEXIST:
  132. log.error([er.message
  133. ,"File exists: "+er.path
  134. ,"Move it away, and try again."].join("\n"))
  135. break
  136. default:
  137. log.error(er)
  138. log.error(["Report this *entire* log at:"
  139. ," <http://github.com/isaacs/npm/issues>"
  140. ,"or email it to:"
  141. ," <npm-@googlegroups.com>"
  142. ].join("\n"))
  143. break
  144. }
  145. var os = require("os")
  146. log.error("")
  147. log.error(os.type() + " " + os.release(), "System")
  148. log.error(process.argv
  149. .map(JSON.stringify).join(" "), "command")
  150. log.error(process.cwd(), "cwd")
  151. log.error(process.version, "node -v")
  152. log.error(npm.version, "npm -v")
  153. exit(typeof er.errno === "number" ? er.errno : 1)
  154. }
  155. function exit (code) {
  156. var doExit = npm.config.get("_exit")
  157. log.verbose([code, doExit], "exit")
  158. if (code) writeLogFile(reallyExit)
  159. else rm("npm-debug.log", function () { rm(npm.tmp, reallyExit) })
  160. function reallyExit() {
  161. itWorked = !code
  162. //if (!itWorked) {
  163. if (!doExit) process.emit("exit", code)
  164. else process.exit(code)
  165. //}
  166. }
  167. }
  168. function writeLogFile (cb) {
  169. var fs = require("graceful-fs")
  170. , fstr = fs.createWriteStream("npm-debug.log")
  171. , util = require("util")
  172. log.history.forEach(function (m) {
  173. var lvl = log.LEVEL[m.level]
  174. , pref = m.pref ? " " + m.pref : ""
  175. , b = lvl + pref + " "
  176. , eol = process.platform === "win32" ? "\r\n" : "\n"
  177. , msg = typeof m.msg === "string" ? m.msg
  178. : msg instanceof Error ? msg.stack || msg.message
  179. : util.inspect(m.msg, 0, 4)
  180. fstr.write(new Buffer(b
  181. +(msg.split(/\r?\n+/).join(eol+b))
  182. + eol))
  183. })
  184. fstr.end()
  185. fstr.on("close", cb)
  186. }