Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add throws option for readFile (async) #39

Merged
merged 5 commits into from
Apr 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,34 @@ function readFile (file, options, callback) {
options = {}
}

if (typeof options === 'string') {
options = {encoding: options}
}

if (options == null) {
options = {}
}

var shouldThrow = true
if ('passParsingErrors' in options) {
shouldThrow = options.passParsingErrors
} else if ('throws' in options) {
shouldThrow = options.throws
}

fs.readFile(file, options, function (err, data) {
if (err) return callback(err)

var obj
try {
obj = JSON.parse(data, options ? options.reviver : null)
} catch (err2) {
err2.message = file + ': ' + err2.message
return callback(err2)
if (shouldThrow) {
err2.message = file + ': ' + err2.message
return callback(err2)
} else {
return callback(null, null)
}
}

callback(null, obj)
Expand All @@ -27,7 +46,13 @@ function readFileSync (file, options) {
options = {encoding: options}
}

var shouldThrow = 'throws' in options ? options.throws : true
var shouldThrow = true
if ('passParsingErrors' in options) {
shouldThrow = options.passParsingErrors
} else if ('throws' in options) {
shouldThrow = options.throws
}

var content = fs.readFileSync(file, options)

try {
Expand Down
31 changes: 31 additions & 0 deletions test/read-file-sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ describe('+ readFileSync()', function () {
})
})

describe('> when invalid JSON and passParsingErrors set to false', function () {
it('should return null', function () {
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
var data = '{not valid JSON'
fs.writeFileSync(file, data)

assert.throws(function () {
jf.readFileSync(file)
})

var obj = jf.readFileSync(file, {passParsingErrors: false})
assert.strictEqual(obj, null)
})
})

describe('> when invalid JSON and throws set to false', function () {
it('should return null', function () {
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
Expand All @@ -64,6 +79,22 @@ describe('+ readFileSync()', function () {
})
})

describe('> when invalid JSON and passParsingErrors set to true', function () {
it('should return null', function () {
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
var data = '{not valid JSON'
fs.writeFileSync(file, data)

assert.throws(function () {
jf.readFileSync(file)
})

assert.throws(function () {
jf.readFileSync(file, {throws: true})
})
})
})

describe('> when invalid JSON and throws set to true', function () {
it('should return null', function () {
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
Expand Down
108 changes: 108 additions & 0 deletions test/read-file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,114 @@ describe('+ readFile()', function () {
})
})

describe('> when invalid JSON and passParsingErrors set to false', function () {
it('should return null and no error', function (done) {
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
var data = '{not valid JSON'
var bothDone = false
fs.writeFileSync(file, data)

jf.readFile(file, function (err, obj2) {
assert(err instanceof Error)
assert(err.message.match(file))
if (bothDone) {
done()
}
bothDone = true
})

jf.readFile(file, {passParsingErrors: false}, function (err, obj2) {
assert.ifError(err)
assert.strictEqual(obj2, null)
if (bothDone) {
done()
}
bothDone = true
})
})
})

describe('> when invalid JSON and throws set to false', function () {
it('should return null and no error', function (done) {
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
var data = '{not valid JSON'
var bothDone = false
fs.writeFileSync(file, data)

jf.readFile(file, function (err, obj2) {
assert(err instanceof Error)
assert(err.message.match(file))
if (bothDone) {
done()
}
bothDone = true
})

jf.readFile(file, {throws: false}, function (err, obj2) {
assert.ifError(err)
assert.strictEqual(obj2, null)
if (bothDone) {
done()
}
bothDone = true
})
})
})

describe('> when invalid JSON and passParsingErrors set to true', function () {
it('should return an error', function (done) {
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
var data = '{not valid JSON'
var bothDone = false
fs.writeFileSync(file, data)

jf.readFile(file, function (err, obj2) {
assert(err instanceof Error)
assert(err.message.match(file))
if (bothDone) {
done()
}
bothDone = true
})

jf.readFile(file, {passParsingErrors: true}, function (err, obj2) {
assert(err instanceof Error)
assert(err.message.match(file))
if (bothDone) {
done()
}
bothDone = true
})
})
})

describe('> when invalid JSON and throws set to true', function () {
it('should return an error', function (done) {
var file = path.join(TEST_DIR, 'somefile4-invalid.json')
var data = '{not valid JSON'
var bothDone = false
fs.writeFileSync(file, data)

jf.readFile(file, function (err, obj2) {
assert(err instanceof Error)
assert(err.message.match(file))
if (bothDone) {
done()
}
bothDone = true
})

jf.readFile(file, {throws: true}, function (err, obj2) {
assert(err instanceof Error)
assert(err.message.match(file))
if (bothDone) {
done()
}
bothDone = true
})
})
})

describe('> when JSON reviver is set', function () {
it('should transform the JSON', function (done) {
var file = path.join(TEST_DIR, 'somefile.json')
Expand Down