Skip to content

Commit 987a3a9

Browse files
committed
feat(csv-parse): on_skip catch thrown error
1 parent f6e41c5 commit 987a3a9

2 files changed

Lines changed: 37 additions & 8 deletions

File tree

packages/csv-parse/lib/api/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -861,10 +861,14 @@ const transform = function (original_options = {}) {
861861
if (skip_records_with_error) {
862862
this.state.recordHasError = true;
863863
if (this.options.on_skip !== undefined) {
864-
this.options.on_skip(
865-
err,
866-
raw ? this.state.rawBuffer.toString(encoding) : undefined,
867-
);
864+
try {
865+
this.options.on_skip(
866+
err,
867+
raw ? this.state.rawBuffer.toString(encoding) : undefined,
868+
);
869+
} catch (err) {
870+
return err;
871+
}
868872
}
869873
// this.emit('skip', err, raw ? this.state.rawBuffer.toString(encoding) : undefined);
870874
return undefined;

packages/csv-parse/test/option.on_skip.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import "should";
2+
import dedent from "dedent";
23
import { parse } from "../lib/index.js";
34
import { assert_error } from "./api.assert_error.js";
45

@@ -10,6 +11,27 @@ describe("Option `on_skip`", function () {
1011
}).should.throw("Invalid Option: on_skip must be a function, got 1");
1112
});
1213

14+
it("catch thrown error", function (next) {
15+
parse(
16+
dedent`
17+
a,b,c,d
18+
invalid
19+
e,f,g,h
20+
`,
21+
{
22+
bom: true,
23+
skip_records_with_error: true,
24+
on_skip: () => {
25+
throw Error("Catchme");
26+
},
27+
},
28+
(err) => {
29+
err.message.should.eql("Catchme");
30+
next();
31+
},
32+
);
33+
});
34+
1335
it('handle "CSV_RECORD_INCONSISTENT_FIELDS_LENGTH" with bom (fix #411)', function (next) {
1436
let errors = 0;
1537
const parser = parse(
@@ -36,10 +58,13 @@ describe("Option `on_skip`", function () {
3658
next(err);
3759
},
3860
);
39-
parser.write(`a,b,c,d
40-
1,2,3
41-
e,f,g,h
42-
`);
61+
parser.write(
62+
dedent`
63+
a,b,c,d
64+
1,2,3
65+
e,f,g,h
66+
`,
67+
);
4368
parser.end();
4469
});
4570
});

0 commit comments

Comments
 (0)