diff options
author | isaacs <i@izs.me> | 2010-10-02 12:16:31 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2010-10-02 12:20:49 +0400 |
commit | c445a291a51d0cafdf453b61d6e129749dc10120 (patch) | |
tree | 8d86f77dcf944b055621417b4ade8edd3568a507 | |
parent | a72ebbde52a7c86d64982fae5aa6179e9c16b946 (diff) |
Move error handling into a separate file, and add more support for common issues.
-rwxr-xr-x | cli.js | 40 | ||||
-rw-r--r-- | lib/utils/error-handler.js | 66 | ||||
-rw-r--r-- | lib/utils/lifecycle.js | 8 |
3 files changed, 74 insertions, 40 deletions
@@ -12,6 +12,7 @@ var fs = require("./lib/utils/graceful-fs") , npm = require("./npm") , ini = require("./lib/utils/ini") , rm = require("./lib/utils/rm-rf") + , errorHandler = require("./lib/utils/error-handler") // supported commands. , argv = process.argv.slice(2) @@ -64,10 +65,6 @@ if (!semver.satisfies(nodeVer, reqVer)) { } process.on("uncaughtException", errorHandler) -process.on("exit", function () { if (!itWorked) log.win("not ok") }) - -var itWorked = false - if (!command && !printVersion) conf.usage = true @@ -83,38 +80,3 @@ else { npm.commands[command](arglist, errorHandler) }) } - -var cbCalled = false -function errorHandler (er) { - if (cbCalled) throw new Error("Callback called more than once.") - cbCalled = true - if (!er) { - itWorked = true - log.win("ok") - return exit() - } - log.error(er) - if (!(er instanceof Error)) return exit(1) - if (er.message.trim() === "ECONNREFUSED, Could not contact DNS servers") { - log.error(["If you are using Cygwin, please set up your /etc/resolv.conf" - ,"See step 3 in this wiki page:" - ," http://github.com/ry/node/wiki/Building-node.js-on-Cygwin-%28Windows%29" - ,"If you are not using Cygwin, please report this" - ,"at <http://github.com/isaacs/npm/issues>" - ,"or email it to <npm-@googlegroups.com>" - ].join("\n")) - } else { - if (npm.commands[command].usage) { - log.error(npm.commands[command].usage) - } - log.error(["try running: 'npm help "+command+"'" - ,"Report this *entire* log at <http://github.com/isaacs/npm/issues>" - ,"or email it to <npm-@googlegroups.com>" - ].join("\n")) - } - exit(1) -} - -function exit (code) { - rm(npm.tmp, function () { process.exit(code || 0) }) -} diff --git a/lib/utils/error-handler.js b/lib/utils/error-handler.js new file mode 100644 index 000000000..209a6a300 --- /dev/null +++ b/lib/utils/error-handler.js @@ -0,0 +1,66 @@ + +module.exports = errorHandler + +var cbCalled = false + , log = require("./log") + , npm = require("../../npm") + , rm = require("./rm-rf") + , constants + , itWorked = false + +try { constants = require("constants") } +catch (ex) { constants = process } + +process.on("exit", function () { if (!itWorked) log.win("not ok") }) + +function errorHandler (er) { + if (cbCalled) throw new Error("Callback called more than once.") + cbCalled = true + if (!er) { + itWorked = true + log.win("ok") + return exit() + } + log.error(er) + if (!(er instanceof Error)) return exit(1) + switch (er.errno) { + case constants.ECONNREFUSED: + log.error(["If you are using Cygwin, please set up your /etc/resolv.conf" + ,"See step 3 in this wiki page:" + ," http://github.com/ry/node/wiki/Building-node.js-on-Cygwin-%28Windows%29" + ,"If you are not using Cygwin, please report this" + ,"at <http://github.com/isaacs/npm/issues>" + ,"or email it to <npm-@googlegroups.com>" + ].join("\n")) + break + case constants.EACCES: + log.error(["There appear to be some permission problems" + ,"See the section on 'Permission Errors' at" + ," http://github.com/isaacs/npm#readme" + ,"This will get better in the future, I promise." + ].join("\n")) + break + case npm.ELIFECYCLE: + log.error(["","Failed at the "+er.pkgid+" "+er.stage+" script." + ,"This is most likely a problem with the "+er.pkgname+"package," + ,"not with npm itself." + ,"Tell the author that this fails on your system:" + ," "+er.script + ,"You can get their info via:" + ," npm owner ls "+er.pkgname + ,"There may be additional logging output above." + ].join("\n")) + break + default: + log.error(["try running: 'npm help "+command+"'" + ,"Report this *entire* log at <http://github.com/isaacs/npm/issues>" + ,"or email it to <npm-@googlegroups.com>" + ,"Just tweeting a tiny part of the error will not be helpful." + ].join("\n")) + } + exit(1) +} + +function exit (code) { + rm(npm.tmp, function () { process.exit(code || 0) }) +} diff --git a/lib/utils/lifecycle.js b/lib/utils/lifecycle.js index ddfb05254..3974fe25e 100644 --- a/lib/utils/lifecycle.js +++ b/lib/utils/lifecycle.js @@ -53,9 +53,15 @@ function runPackageLifecycle (pkg, env, cb) { log("Failed to exec "+stage+" script", pkg._id) er.message = pkg._id + " " + stage + ": `" + env.npm_lifecycle_script+"`\n" + er.message + er.errno = npm.ELIFECYCLE + er.pkgid = pkg._id + er.stage = stage + er.script = env.npm_lifecycle_script + er.pkgname = pkg.name return cb(er) } else if (er) { - log.error(er, pkg._id+"."+stage+" failed") + log.error(er, pkg._id+"."+stage) + log.error("failed, but continuing anyway", pkg._id+"."+stage) } cb(er) }) |