Skip to content

Loading…

Fix processing of errors during parsing #25

Open
wants to merge 1 commit into from

1 participant

@vdmitriy

No description provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 30, 2013
  1. Fix processing of errors during parsing

    Dmitriy Voeykov committed
Showing with 19 additions and 10 deletions.
  1. +19 −10 lib/ya-csv.js
View
29 lib/ya-csv.js
@@ -1,10 +1,5 @@
-var sys;
-try {
- sys = require('util');
-} catch (e) {
- sys = require('sys');
-}
-var events = require('events'),
+var sys = require('util'),
+ events = require('events'),
fs = require('fs');
var csv = exports;
@@ -42,7 +37,7 @@ var CsvReader = csv.CsvReader = function(readStream, options) {
};
if (readStream) {
- readStream.addListener('data', this.parse.bind(this));
+ readStream.addListener('data', this.parse.bind(this));
readStream.addListener('error', this.emit.bind(this, 'error'));
readStream.addListener('end', this.end.bind(this));
@@ -86,6 +81,19 @@ var CsvReader = csv.CsvReader = function(readStream, options) {
readstream.destroy();
return self;
}
+
+ /**
+ * Break listeners when error occured during parsing process
+ * @method onerror
+ * @return event `error`
+ */
+ self.onerror = function(err) {
+ readStream.removeListener('data',self.parse.bind(self));
+ readStream.removeListener('error',self.emit.bind(self, 'error'));
+ readStream.removeListener('end',self.end.bind(self));
+ self.destroy();
+ self.emit('error',err);
+ }
}
};
@@ -128,8 +136,8 @@ CsvReader.prototype.parse = function(data) {
// closing quote should be followed by separator unless the nested quotes option is set
var nextChar = data.charAt(i + 1);
if (nextChar && nextChar != '\r' && nextChar != '\n' && nextChar !== this.separator && this.nestedQuotes != true) {
- throw new Error("separator expected after a closing quote; found " + nextChar);
- } else {
+ return this.onerror(new Error("separator expected after a closing quote; found " + nextChar));
+ } else {
ps.quotedField = false;
}
} else if (ps.openField === '') {
@@ -191,6 +199,7 @@ CsvReader.prototype.end = function() {
this.emit('end');
}
}
+
CsvReader.prototype._isEscapable = function(c) {
if ((c === this.escapechar) || (c === this.quotechar)) {
return true;
Something went wrong with that request. Please try again.