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

Commit 18f68cf

Browse files
committed
on_record: catch and handle user errors
1 parent 1928f35 commit 18f68cf

File tree

4 files changed

+76
-14
lines changed

4 files changed

+76
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
* promise: new API module
1111
* errors: finish normalisation of all errors
1212

13+
## Trunk
14+
15+
* on_record: catch and handle user errors
16+
1317
## Version 4.8.5
1418

1519
* ts: fix `types` declaration

lib/es5/index.js

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -869,40 +869,64 @@ function (_Transform) {
869869

870870
if (objname === undefined) {
871871
if (raw === true || info === true) {
872-
this.__push(Object.assign({
872+
var _err6 = this.__push(Object.assign({
873873
record: obj
874874
}, raw === true ? {
875875
raw: this.state.rawBuffer.toString()
876876
} : {}, info === true ? {
877877
info: this.state.info
878878
} : {}));
879+
880+
if (_err6) {
881+
return _err6;
882+
}
879883
} else {
880-
this.__push(obj);
884+
var _err7 = this.__push(obj);
885+
886+
if (_err7) {
887+
return _err7;
888+
}
881889
}
882890
} else {
883891
if (raw === true || info === true) {
884-
this.__push(Object.assign({
892+
var _err8 = this.__push(Object.assign({
885893
record: [obj[objname], obj]
886894
}, raw === true ? {
887895
raw: this.state.rawBuffer.toString()
888896
} : {}, info === true ? {
889897
info: this.state.info
890898
} : {}));
899+
900+
if (_err8) {
901+
return _err8;
902+
}
891903
} else {
892-
this.__push([obj[objname], obj]);
904+
var _err9 = this.__push([obj[objname], obj]);
905+
906+
if (_err9) {
907+
return _err9;
908+
}
893909
}
894910
}
895911
} else {
896912
if (raw === true || info === true) {
897-
this.__push(Object.assign({
913+
var _err10 = this.__push(Object.assign({
898914
record: record
899915
}, raw === true ? {
900916
raw: this.state.rawBuffer.toString()
901917
} : {}, info === true ? {
902918
info: this.state.info
903919
} : {}));
920+
921+
if (_err10) {
922+
return _err10;
923+
}
904924
} else {
905-
this.__push(record);
925+
var _err11 = this.__push(record);
926+
927+
if (_err11) {
928+
return _err11;
929+
}
906930
}
907931
}
908932
}
@@ -998,7 +1022,11 @@ function (_Transform) {
9981022
if (on_record !== undefined) {
9991023
var context = this.__context();
10001024

1001-
record = on_record.call(null, record, context);
1025+
try {
1026+
record = on_record.call(null, record, context);
1027+
} catch (err) {
1028+
return err;
1029+
}
10021030

10031031
if (record === undefined || record === null) {
10041032
return;

lib/index.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -733,34 +733,52 @@ class Parser extends Transform {
733733
const {objname} = this.options
734734
if(objname === undefined){
735735
if(raw === true || info === true){
736-
this.__push(Object.assign(
736+
const err = this.__push(Object.assign(
737737
{record: obj},
738738
(raw === true ? {raw: this.state.rawBuffer.toString()}: {}),
739739
(info === true ? {info: this.state.info}: {})
740740
))
741+
if(err){
742+
return err
743+
}
741744
}else{
742-
this.__push(obj)
745+
const err = this.__push(obj)
746+
if(err){
747+
return err
748+
}
743749
}
744750
}else{
745751
if(raw === true || info === true){
746-
this.__push(Object.assign(
752+
const err = this.__push(Object.assign(
747753
{record: [obj[objname], obj]},
748754
raw === true ? {raw: this.state.rawBuffer.toString()}: {},
749755
info === true ? {info: this.state.info}: {}
750756
))
757+
if(err){
758+
return err
759+
}
751760
}else{
752-
this.__push([obj[objname], obj])
761+
const err = this.__push([obj[objname], obj])
762+
if(err){
763+
return err
764+
}
753765
}
754766
}
755767
}else{
756768
if(raw === true || info === true){
757-
this.__push(Object.assign(
769+
const err = this.__push(Object.assign(
758770
{record: record},
759771
raw === true ? {raw: this.state.rawBuffer.toString()}: {},
760772
info === true ? {info: this.state.info}: {}
761773
))
774+
if(err){
775+
return err
776+
}
762777
}else{
763-
this.__push(record)
778+
const err = this.__push(record)
779+
if(err){
780+
return err
781+
}
764782
}
765783
}
766784
}
@@ -828,7 +846,11 @@ class Parser extends Transform {
828846
const {on_record} = this.options
829847
if(on_record !== undefined){
830848
const context = this.__context()
831-
record = on_record.call(null, record, context)
849+
try{
850+
record = on_record.call(null, record, context)
851+
}catch(err){
852+
return err
853+
}
832854
if(record === undefined || record === null){ return }
833855
}
834856
this.push(record)

test/option.on_record.coffee

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ describe 'Option `on_record`', ->
2222
if lines is 2 then null else record
2323
, (err, records) ->
2424
records.should.eql [ ['a', 'b'], ['e', 'f'] ]
25+
26+
it 'honors skip_lines_with_error', ->
27+
parse "a,b\nc,d\ne,f",
28+
on_record: (record, {lines}) ->
29+
if lines is 2 then throw Error 'Error thrown on line 2' else record
30+
skip_lines_with_error: true
31+
, (err, records) ->
32+
err.message.should.eql 'Error thrown on line 2'

0 commit comments

Comments
 (0)