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

Set error name to SyntaxError #73

Merged
merged 5 commits into from
Aug 18, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ Parser.prototype._online = function (buf, start, end) {
}

if (this.strict && cells.length !== this.headers.length) {
this.emit('error', new Error('Row length does not match headers'))
var e = new Error('Row length does not match headers')
e.name = 'SyntaxError'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe that SyntaxError is correct here. According to the Node docs: https://nodejs.org/api/errors.html#errors_class_syntaxerror, this is used only for invalid JavaScript , or as a result of code evaluation.

RangeError should be more appropriate https://nodejs.org/api/errors.html#errors_class_rangeerror. And it's a built-in, so no need to set the name property, just instantiate directly.

this.emit('error', e)
} else {
this._emit(this._Row, cells)
}
Expand Down
5 changes: 3 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var test = require('tape')
var fs = require('fs')
var path = require('path')
var eol = require('os').EOL
var bops = require('bops')
var spectrum = require('csv-spectrum')
var concat = require('concat-stream')
var csv = require('..')
var read = fs.createReadStream
var eol = '\n'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Windows EOL is '\r\n', which causes a few tests to fail. Making this '\n' across all operating systems fixes this problem.


test('simple csv', function (t) {
collect('dummy.csv', verify)
Expand Down Expand Up @@ -235,7 +235,8 @@ test('custom newline', function (t) {
test('optional strict', function (t) {
collect('test_strict.csv', {strict: true}, verify)
function verify (err, lines) {
t.equal(err.message, 'Row length does not match headers', 'strict row length')
t.equal(err.name, 'SyntaxError', 'err name')
t.equal(err.message, 'Row length does not match headers', 'err message')
t.same(lines[0], {a: '1', b: '2', c: '3'}, 'first row')
t.same(lines[1], {a: '4', b: '5', c: '6'}, 'second row')
t.equal(lines.length, 2, '2 rows before error')
Expand Down