Skip to content

Commit

Permalink
fix(csv-parse): improve INVALID_OPENING_QUOTE error message (fix #120)
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Nov 28, 2022
1 parent 9fcd6da commit 2189bfb
Show file tree
Hide file tree
Showing 17 changed files with 87 additions and 47 deletions.
11 changes: 7 additions & 4 deletions packages/csv-parse/dist/cjs/index.cjs
Expand Up @@ -8,7 +8,7 @@ const is_object = function(obj){

class CsvError extends Error {
constructor(code, message, options, ...contexts) {
if(Array.isArray(message)) message = message.join(' ');
if(Array.isArray(message)) message = message.join(' ').trim();
super(message);
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError);
Expand Down Expand Up @@ -639,7 +639,7 @@ const transform = function(original_options = {}) {
},
// Central parser implementation
parse: function(nextBuf, end, push, close){
const {bom, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
const {bom, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
let {comment, escape, quote, record_delimiter} = this.options;
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
let buf;
Expand Down Expand Up @@ -775,11 +775,14 @@ const transform = function(original_options = {}) {
if(this.state.field.length !== 0){
// In relax_quotes mode, treat opening quote preceded by chrs as regular
if(relax_quotes === false){
const info = this.__infoField();
const bom = Object.keys(boms).map(b => boms[b].equals(this.state.field.toString()) ? b : false).filter(Boolean)[0];
const err = this.__error(
new CsvError('INVALID_OPENING_QUOTE', [
'Invalid Opening Quote:',
`a quote is found inside a field at line ${this.info.lines}`,
], this.options, this.__infoField(), {
`a quote is found on field ${JSON.stringify(info.column)} at line ${info.lines}, value is ${JSON.stringify(this.state.field.toString(encoding))}`,
bom ? `(${bom} bom)` : undefined
], this.options, info, {
field: this.state.field,
})
);
Expand Down
11 changes: 7 additions & 4 deletions packages/csv-parse/dist/cjs/sync.cjs
Expand Up @@ -2,7 +2,7 @@

class CsvError extends Error {
constructor(code, message, options, ...contexts) {
if(Array.isArray(message)) message = message.join(' ');
if(Array.isArray(message)) message = message.join(' ').trim();
super(message);
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError);
Expand Down Expand Up @@ -637,7 +637,7 @@ const transform = function(original_options = {}) {
},
// Central parser implementation
parse: function(nextBuf, end, push, close){
const {bom, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
const {bom, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
let {comment, escape, quote, record_delimiter} = this.options;
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
let buf;
Expand Down Expand Up @@ -773,11 +773,14 @@ const transform = function(original_options = {}) {
if(this.state.field.length !== 0){
// In relax_quotes mode, treat opening quote preceded by chrs as regular
if(relax_quotes === false){
const info = this.__infoField();
const bom = Object.keys(boms).map(b => boms[b].equals(this.state.field.toString()) ? b : false).filter(Boolean)[0];
const err = this.__error(
new CsvError('INVALID_OPENING_QUOTE', [
'Invalid Opening Quote:',
`a quote is found inside a field at line ${this.info.lines}`,
], this.options, this.__infoField(), {
`a quote is found on field ${JSON.stringify(info.column)} at line ${info.lines}, value is ${JSON.stringify(this.state.field.toString(encoding))}`,
bom ? `(${bom} bom)` : undefined
], this.options, info, {
field: this.state.field,
})
);
Expand Down
11 changes: 7 additions & 4 deletions packages/csv-parse/dist/esm/index.js
Expand Up @@ -5066,7 +5066,7 @@ const is_object = function(obj){

class CsvError extends Error {
constructor(code, message, options, ...contexts) {
if(Array.isArray(message)) message = message.join(' ');
if(Array.isArray(message)) message = message.join(' ').trim();
super(message);
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError);
Expand Down Expand Up @@ -5697,7 +5697,7 @@ const transform = function(original_options = {}) {
},
// Central parser implementation
parse: function(nextBuf, end, push, close){
const {bom, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
const {bom, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
let {comment, escape, quote, record_delimiter} = this.options;
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
let buf;
Expand Down Expand Up @@ -5833,11 +5833,14 @@ const transform = function(original_options = {}) {
if(this.state.field.length !== 0){
// In relax_quotes mode, treat opening quote preceded by chrs as regular
if(relax_quotes === false){
const info = this.__infoField();
const bom = Object.keys(boms).map(b => boms[b].equals(this.state.field.toString()) ? b : false).filter(Boolean)[0];
const err = this.__error(
new CsvError('INVALID_OPENING_QUOTE', [
'Invalid Opening Quote:',
`a quote is found inside a field at line ${this.info.lines}`,
], this.options, this.__infoField(), {
`a quote is found on field ${JSON.stringify(info.column)} at line ${info.lines}, value is ${JSON.stringify(this.state.field.toString(encoding))}`,
bom ? `(${bom} bom)` : undefined
], this.options, info, {
field: this.state.field,
})
);
Expand Down
11 changes: 7 additions & 4 deletions packages/csv-parse/dist/esm/sync.js
Expand Up @@ -1972,7 +1972,7 @@ function isSlowBuffer (obj) {

class CsvError extends Error {
constructor(code, message, options, ...contexts) {
if(Array.isArray(message)) message = message.join(' ');
if(Array.isArray(message)) message = message.join(' ').trim();
super(message);
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError);
Expand Down Expand Up @@ -2607,7 +2607,7 @@ const transform = function(original_options = {}) {
},
// Central parser implementation
parse: function(nextBuf, end, push, close){
const {bom, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
const {bom, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
let {comment, escape, quote, record_delimiter} = this.options;
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
let buf;
Expand Down Expand Up @@ -2743,11 +2743,14 @@ const transform = function(original_options = {}) {
if(this.state.field.length !== 0){
// In relax_quotes mode, treat opening quote preceded by chrs as regular
if(relax_quotes === false){
const info = this.__infoField();
const bom = Object.keys(boms).map(b => boms[b].equals(this.state.field.toString()) ? b : false).filter(Boolean)[0];
const err = this.__error(
new CsvError('INVALID_OPENING_QUOTE', [
'Invalid Opening Quote:',
`a quote is found inside a field at line ${this.info.lines}`,
], this.options, this.__infoField(), {
`a quote is found on field ${JSON.stringify(info.column)} at line ${info.lines}, value is ${JSON.stringify(this.state.field.toString(encoding))}`,
bom ? `(${bom} bom)` : undefined
], this.options, info, {
field: this.state.field,
})
);
Expand Down
11 changes: 7 additions & 4 deletions packages/csv-parse/dist/iife/index.js
Expand Up @@ -5069,7 +5069,7 @@ var csv_parse = (function (exports) {

class CsvError extends Error {
constructor(code, message, options, ...contexts) {
if(Array.isArray(message)) message = message.join(' ');
if(Array.isArray(message)) message = message.join(' ').trim();
super(message);
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError);
Expand Down Expand Up @@ -5700,7 +5700,7 @@ var csv_parse = (function (exports) {
},
// Central parser implementation
parse: function(nextBuf, end, push, close){
const {bom, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
const {bom, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
let {comment, escape, quote, record_delimiter} = this.options;
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
let buf;
Expand Down Expand Up @@ -5836,11 +5836,14 @@ var csv_parse = (function (exports) {
if(this.state.field.length !== 0){
// In relax_quotes mode, treat opening quote preceded by chrs as regular
if(relax_quotes === false){
const info = this.__infoField();
const bom = Object.keys(boms).map(b => boms[b].equals(this.state.field.toString()) ? b : false).filter(Boolean)[0];
const err = this.__error(
new CsvError('INVALID_OPENING_QUOTE', [
'Invalid Opening Quote:',
`a quote is found inside a field at line ${this.info.lines}`,
], this.options, this.__infoField(), {
`a quote is found on field ${JSON.stringify(info.column)} at line ${info.lines}, value is ${JSON.stringify(this.state.field.toString(encoding))}`,
bom ? `(${bom} bom)` : undefined
], this.options, info, {
field: this.state.field,
})
);
Expand Down
11 changes: 7 additions & 4 deletions packages/csv-parse/dist/iife/sync.js
Expand Up @@ -1975,7 +1975,7 @@ var csv_parse_sync = (function (exports) {

class CsvError extends Error {
constructor(code, message, options, ...contexts) {
if(Array.isArray(message)) message = message.join(' ');
if(Array.isArray(message)) message = message.join(' ').trim();
super(message);
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError);
Expand Down Expand Up @@ -2610,7 +2610,7 @@ var csv_parse_sync = (function (exports) {
},
// Central parser implementation
parse: function(nextBuf, end, push, close){
const {bom, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
const {bom, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
let {comment, escape, quote, record_delimiter} = this.options;
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
let buf;
Expand Down Expand Up @@ -2746,11 +2746,14 @@ var csv_parse_sync = (function (exports) {
if(this.state.field.length !== 0){
// In relax_quotes mode, treat opening quote preceded by chrs as regular
if(relax_quotes === false){
const info = this.__infoField();
const bom = Object.keys(boms).map(b => boms[b].equals(this.state.field.toString()) ? b : false).filter(Boolean)[0];
const err = this.__error(
new CsvError('INVALID_OPENING_QUOTE', [
'Invalid Opening Quote:',
`a quote is found inside a field at line ${this.info.lines}`,
], this.options, this.__infoField(), {
`a quote is found on field ${JSON.stringify(info.column)} at line ${info.lines}, value is ${JSON.stringify(this.state.field.toString(encoding))}`,
bom ? `(${bom} bom)` : undefined
], this.options, info, {
field: this.state.field,
})
);
Expand Down
11 changes: 7 additions & 4 deletions packages/csv-parse/dist/umd/index.js
Expand Up @@ -5072,7 +5072,7 @@

class CsvError extends Error {
constructor(code, message, options, ...contexts) {
if(Array.isArray(message)) message = message.join(' ');
if(Array.isArray(message)) message = message.join(' ').trim();
super(message);
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError);
Expand Down Expand Up @@ -5703,7 +5703,7 @@
},
// Central parser implementation
parse: function(nextBuf, end, push, close){
const {bom, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
const {bom, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
let {comment, escape, quote, record_delimiter} = this.options;
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
let buf;
Expand Down Expand Up @@ -5839,11 +5839,14 @@
if(this.state.field.length !== 0){
// In relax_quotes mode, treat opening quote preceded by chrs as regular
if(relax_quotes === false){
const info = this.__infoField();
const bom = Object.keys(boms).map(b => boms[b].equals(this.state.field.toString()) ? b : false).filter(Boolean)[0];
const err = this.__error(
new CsvError('INVALID_OPENING_QUOTE', [
'Invalid Opening Quote:',
`a quote is found inside a field at line ${this.info.lines}`,
], this.options, this.__infoField(), {
`a quote is found on field ${JSON.stringify(info.column)} at line ${info.lines}, value is ${JSON.stringify(this.state.field.toString(encoding))}`,
bom ? `(${bom} bom)` : undefined
], this.options, info, {
field: this.state.field,
})
);
Expand Down
11 changes: 7 additions & 4 deletions packages/csv-parse/dist/umd/sync.js
Expand Up @@ -1978,7 +1978,7 @@

class CsvError extends Error {
constructor(code, message, options, ...contexts) {
if(Array.isArray(message)) message = message.join(' ');
if(Array.isArray(message)) message = message.join(' ').trim();
super(message);
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError);
Expand Down Expand Up @@ -2613,7 +2613,7 @@
},
// Central parser implementation
parse: function(nextBuf, end, push, close){
const {bom, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
const {bom, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
let {comment, escape, quote, record_delimiter} = this.options;
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
let buf;
Expand Down Expand Up @@ -2749,11 +2749,14 @@
if(this.state.field.length !== 0){
// In relax_quotes mode, treat opening quote preceded by chrs as regular
if(relax_quotes === false){
const info = this.__infoField();
const bom = Object.keys(boms).map(b => boms[b].equals(this.state.field.toString()) ? b : false).filter(Boolean)[0];
const err = this.__error(
new CsvError('INVALID_OPENING_QUOTE', [
'Invalid Opening Quote:',
`a quote is found inside a field at line ${this.info.lines}`,
], this.options, this.__infoField(), {
`a quote is found on field ${JSON.stringify(info.column)} at line ${info.lines}, value is ${JSON.stringify(this.state.field.toString(encoding))}`,
bom ? `(${bom} bom)` : undefined
], this.options, info, {
field: this.state.field,
})
);
Expand Down
2 changes: 1 addition & 1 deletion packages/csv-parse/lib/api/CsvError.js
@@ -1,7 +1,7 @@

class CsvError extends Error {
constructor(code, message, options, ...contexts) {
if(Array.isArray(message)) message = message.join(' ');
if(Array.isArray(message)) message = message.join(' ').trim();
super(message);
if(Error.captureStackTrace !== undefined){
Error.captureStackTrace(this, CsvError);
Expand Down
9 changes: 6 additions & 3 deletions packages/csv-parse/lib/api/index.js
Expand Up @@ -61,7 +61,7 @@ const transform = function(original_options = {}) {
},
// Central parser implementation
parse: function(nextBuf, end, push, close){
const {bom, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
const {bom, encoding, from_line, ltrim, max_record_size,raw, relax_quotes, rtrim, skip_empty_lines, to, to_line} = this.options;
let {comment, escape, quote, record_delimiter} = this.options;
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state;
let buf;
Expand Down Expand Up @@ -197,11 +197,14 @@ const transform = function(original_options = {}) {
if(this.state.field.length !== 0){
// In relax_quotes mode, treat opening quote preceded by chrs as regular
if(relax_quotes === false){
const info = this.__infoField();
const bom = Object.keys(boms).map(b => boms[b].equals(this.state.field.toString()) ? b : false).filter(Boolean)[0];
const err = this.__error(
new CsvError('INVALID_OPENING_QUOTE', [
'Invalid Opening Quote:',
`a quote is found inside a field at line ${this.info.lines}`,
], this.options, this.__infoField(), {
`a quote is found on field ${JSON.stringify(info.column)} at line ${info.lines}, value is ${JSON.stringify(this.state.field.toString(encoding))}`,
bom ? `(${bom} bom)` : undefined
], this.options, info, {
field: this.state.field,
})
);
Expand Down
2 changes: 1 addition & 1 deletion packages/csv-parse/test/api.events.coffee
Expand Up @@ -46,7 +46,7 @@ describe 'API events', ->
'''
parser = parse (err) ->
assert_error err,
message: 'Invalid Opening Quote: a quote is found inside a field at line 1'
message: 'Invalid Opening Quote: a quote is found on field 0 at line 1, value is " x "'
code: 'INVALID_OPENING_QUOTE'
field: ' x '
next()
Expand Down

0 comments on commit 2189bfb

Please sign in to comment.