From 5d119ae246f27353b14ff063559d1ba8c616bb89 Mon Sep 17 00:00:00 2001 From: Nick Santos Date: Thu, 16 Oct 2014 13:19:45 -0400 Subject: [PATCH] better errors for malformed .npmrc properties --- lib/config/core.js | 27 ++++++++++++++++++++------- test/fixtures/config/malformed | 1 + test/tap/00-config-setup.js | 1 + test/tap/config-malformed.js | 14 ++++++++++++++ 4 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/config/malformed create mode 100644 test/tap/config-malformed.js diff --git a/lib/config/core.js b/lib/config/core.js index c1ccf60897b..6c6112532fa 100644 --- a/lib/config/core.js +++ b/lib/config/core.js @@ -36,7 +36,7 @@ var myGid = process.env.SUDO_GID !== undefined var loading = false var loadCbs = [] -function load (cli_, builtin_, cb_) { +function load () { var cli, builtin, cb for (var i = 0; i < arguments.length; i++) switch (typeof arguments[i]) { @@ -92,6 +92,7 @@ function load (cli_, builtin_, cb_) { rc.on("load", function () { load_(builtin, rc, cli, cb) }) + rc.on("error", cb) } function load_(builtin, rc, cli, cb) { @@ -261,7 +262,7 @@ Conf.prototype.save = function (where, cb) { var mode = where === "user" ? "0600" : "0666" if (!data.trim()) { - fs.unlink(target.path, function (er) { + fs.unlink(target.path, function () { // ignore the possible error (e.g. the file doesn't exist) done(null) }) @@ -321,9 +322,15 @@ Conf.prototype.parse = function (content, file) { } Conf.prototype.add = function (data, marker) { - Object.keys(data).forEach(function (k) { - data[k] = parseField(data[k], k) - }) + try { + Object.keys(data).forEach(function (k) { + data[k] = parseField(data[k], k) + }) + } + catch (e) { + this.emit("error", e) + return this + } return CC.prototype.add.call(this, data, marker) } @@ -360,8 +367,14 @@ function parseField (f, k) { f = (""+f).trim() - if (f.match(/^".*"$/)) - f = JSON.parse(f) + if (f.match(/^".*"$/)) { + try { + f = JSON.parse(f) + } + catch (e) { + throw new Error("Failed parsing JSON config key " + k + ": " + f) + } + } if (isBool && !isString && f === "") return true diff --git a/test/fixtures/config/malformed b/test/fixtures/config/malformed new file mode 100644 index 00000000000..182c4d2c71c --- /dev/null +++ b/test/fixtures/config/malformed @@ -0,0 +1 @@ +email = """ \ No newline at end of file diff --git a/test/tap/00-config-setup.js b/test/tap/00-config-setup.js index 2de5d21ddaa..aaad5462715 100644 --- a/test/tap/00-config-setup.js +++ b/test/tap/00-config-setup.js @@ -3,6 +3,7 @@ var userconfigSrc = path.resolve(__dirname, "..", "fixtures", "config", "usercon exports.userconfig = userconfigSrc + "-with-gc" exports.globalconfig = path.resolve(__dirname, "..", "fixtures", "config", "globalconfig") exports.builtin = path.resolve(__dirname, "..", "fixtures", "config", "builtin") +exports.malformed = path.resolve(__dirname, "..", "fixtures", "config", "malformed") exports.ucData = { globalconfig: exports.globalconfig, email: "i@izs.me", diff --git a/test/tap/config-malformed.js b/test/tap/config-malformed.js new file mode 100644 index 00000000000..04502214621 --- /dev/null +++ b/test/tap/config-malformed.js @@ -0,0 +1,14 @@ +var test = require('tap').test + +var npmconf = require("../../lib/config/core.js") +var common = require("./00-config-setup.js") + +test('with malformed', function (t) { + npmconf.load({}, common.malformed, function (er, conf) { + t.ok(er, 'Expected parse error') + if (!(er && /Failed parsing JSON config key email/.test(er.message))) { + throw er + } + t.end() + }) +})