Skip to content

Commit

Permalink
better error message for writing not lists
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Dec 4, 2015
1 parent 89016f7 commit 71cd65b
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,29 @@ var formatters = {
return JSON.stringify(file)
},
csv: function (file) {
if (!_.isArray(file)) {
reporters.notListError('csv')
}
try {
return dsv.csv.format(file)
} catch (err) {
reporters.parseError('csv')
}
},
tsv: function (file) {
if (!_.isArray(file)) {
reporters.notListError('tsv')
}
try {
return dsv.tsv.format(file)
} catch (err) {
reporters.parseError('tsv')
}
},
psv: function (file) {
if (!_.isArray(file)) {
reporters.notListError('psv')
}
try {
return dsv.dsv('|').format(file)
} catch (err) {
Expand Down Expand Up @@ -90,6 +99,10 @@ reporters.parseError = function (format) {
throw new Error(chalk.red('Error converting your data to ' + chalk.bold(format) + '.') + '\n\n' + chalk.cyan('Your data most likely contains objects or lists. Object values can only be strings for this format. Please convert before writing to file.\n'))
}

reporters.notListError = function (format) {
throw new Error(chalk.red('You passed in an object but converting to ' + chalk.bold(format) + ' requires a list of objects.') + chalk.cyan('\nIf you would like to write a one-row csv, put your object in a list like so: ' + chalk.bold('[data]')+'\n'))
}

/** @namespace */
var helpers = {}

Expand Down

0 comments on commit 71cd65b

Please sign in to comment.