Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Commit

Permalink
errors: don't stringify/parse undefined and null values, fix #262
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Oct 21, 2019
1 parent ad65f3c commit 1f7c5f3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,12 @@
* promise: new API module
* errors: finish normalisation of all errors

## Trunk

* errors: don't stringify/parse undefined and null values
* errors: expose CSV_NON_TRIMABLE_CHAR_AFTER_CLOSING_QUOTE
* errors: expose CSV_MAX_RECORD_SIZE

## Version 4.6.3

* lint: integrate eslint
Expand Down
4 changes: 2 additions & 2 deletions lib/es5/index.js
Expand Up @@ -1216,8 +1216,8 @@ function (_Error) {
var context = _contexts[_i2];

for (var key in context) {
var value = Buffer.isBuffer(context[key]) ? context[key].toString() : context[key];
_this2[key] = JSON.parse(JSON.stringify(value));
var value = context[key];
_this2[key] = Buffer.isBuffer(value) ? value.toString() : value == null ? value : JSON.parse(JSON.stringify(value));
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Expand Up @@ -969,8 +969,8 @@ class CsvError extends Error {
this.code = code
for(const context of contexts){
for(const key in context){
const value = Buffer.isBuffer(context[key]) ? context[key].toString() : context[key]
this[key] = JSON.parse(JSON.stringify(value))
const value = context[key]
this[key] = Buffer.isBuffer(value) ? value.toString() : value == null ? value : JSON.parse(JSON.stringify(value))
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions test/option.columns.coffee
Expand Up @@ -181,6 +181,19 @@ describe 'Option `columns`', ->
{ a: '3' }
] unless err
next err

it '', (next) ->
# Trigger a bug where error is try to stringify and parse an undefined
# value, conjointly triggered by a null column and a
# CSV_INVALID_RECORD_LENGTH_DONT_MATCH_COLUMNS error
parse """
col_a,col_b,col_c
foo,bar
foo,bar,baz
"""
, columns: ['a', 'b', null], (err, data) ->
err.code.should.eql 'CSV_INVALID_RECORD_LENGTH_DONT_MATCH_COLUMNS'
next()

describe 'function', ->

Expand Down
1 change: 1 addition & 0 deletions test/option.skip_lines_with_error.coffee
Expand Up @@ -46,6 +46,7 @@ describe 'Option `skip_lines_with_error`', ->
["line","1"]
["line", "3"]
] unless err
console.log errors
assert_error errors, [
message: 'Invalid Opening Quote: a quote is found inside a field at line 2'
code: 'INVALID_OPENING_QUOTE'
Expand Down

0 comments on commit 1f7c5f3

Please sign in to comment.