Skip to content

Commit

Permalink
Catch errors in 'data' event; Add readable and writable properties;
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Apr 1, 2012
1 parent 8fee268 commit 49d9cf3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
25 changes: 22 additions & 3 deletions lib/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ module.exports = function(){
bufferSize: null,
end: true // Call `end()` on close
};
// A boolean that is true by default, but turns false after an 'error' occurred,
// the stream came to an 'end', or destroy() was called.
this.readable = true;
// A boolean that is true by default, but turns false after an 'error' occurred
// or end() / destroy() was called.
this.writable = true;
}
CSV.prototype.__proto__ = EventEmitter.prototype;

Expand Down Expand Up @@ -92,6 +98,7 @@ module.exports = function(){
parse(data);
}catch(e){
self.emit('error', e);
// Destroy the input stream
this.destroy();
}
});
Expand Down Expand Up @@ -150,9 +157,11 @@ module.exports = function(){
csv.writeStream.end();
}else{
csv.emit('end', state.count);
csv.readable = false;
}
}else{
csv.emit('end', state.count);
csv.readable = false;
}
}
}
Expand All @@ -178,7 +187,9 @@ module.exports = function(){
break;
}
writeStream.on('close', function(){
self.emit('end',state.count);
self.emit('end', state.count);
self.readable = false;
self.writable = false;
})
this.writeStream = writeStream;
state.buffer = new Buffer(this.writeOptions.bufferSize||this.readOptions.bufferSize);
Expand Down Expand Up @@ -219,7 +230,13 @@ module.exports = function(){
return;
}
if(!preserve){
csv.emit('data', line, state.count);
try {
csv.emit('data', line, state.count);
}catch(e){
csv.emit('error', e);
csv.readable = false;
csv.writable = false;
}
}
if(typeof line === 'object'){
if(!(line instanceof Array)){
Expand All @@ -246,7 +263,7 @@ module.exports = function(){
// Cast number to string
field = '' + field;
}else if(typeof field === 'boolean'){
// Cast number to string
// Cast boolean to string
field = field ? '1' : '';
}else if(field instanceof Date){
// Cast date to timestamp string
Expand Down Expand Up @@ -285,6 +302,7 @@ module.exports = function(){
if(!preserve){
state.countWriten++;
}
return true;
}

/**
Expand Down Expand Up @@ -383,6 +401,7 @@ module.exports = function(){
}
}

// Called by the `parse` function on each line. It will then call `write`
function flush(){
if(csv.readOptions.columns){
if(state.count === 0 && csv.readOptions.columns === true){
Expand Down
19 changes: 19 additions & 0 deletions test/reader.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

fs = require 'fs'
should = require 'should'
csv = require '..'

describe 'reader', ->
it 'Test exception in on(data)"', (next) ->
count = 0
errors = 0
test = csv()
.toPath( "#{__dirname}/write/write_array.tmp" )
.on 'data', (data, index) ->
throw new Error 'Error in data' if index % 10 is 0
.on 'error', ->
next()
.on 'end', ->
false.should.be.ok
for i in [0...1000]
test.write ['Test '+i, i, '"'] if test.writable

0 comments on commit 49d9cf3

Please sign in to comment.