Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
better errors for malformed .npmrc properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Santos authored and othiym23 committed Oct 16, 2014
1 parent 53108b2 commit 5d119ae
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
27 changes: 20 additions & 7 deletions lib/config/core.js
Expand Up @@ -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]) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/config/malformed
@@ -0,0 +1 @@
email = """
1 change: 1 addition & 0 deletions test/tap/00-config-setup.js
Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions 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()
})
})

0 comments on commit 5d119ae

Please sign in to comment.