Skip to content

Commit

Permalink
pass errors in streams to the callback (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
lerouxb authored Jul 20, 2023
1 parent 908f587 commit 88658b9
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,32 @@ export class ParseStream extends Duplex {
this.analyzer = new SchemaAnalyzer(options);
}

_write(obj: Document, enc: unknown, cb: () => void) {
this.analyzer.analyzeDoc(obj);
this.emit('progress', obj);
cb();
_write(obj: Document, enc: unknown, cb: (err?: any) => void) {
try {
this.analyzer.analyzeDoc(obj);
this.emit('progress', obj);
cb();
} catch (err: any) {
cb(err);
}
}

_read() {}

_final(cb: () => void) {
if (this.options.schemaPaths) {
this.push(this.analyzer.getSchemaPaths());
} else if (this.options.simplifiedSchema) {
this.push(this.analyzer.getSimplifiedSchema());
} else {
this.push(this.analyzer.getResult());
_final(cb: (err?: any) => void) {
try {
if (this.options.schemaPaths) {
this.push(this.analyzer.getSchemaPaths());
} else if (this.options.simplifiedSchema) {
this.push(this.analyzer.getSimplifiedSchema());
} else {
this.push(this.analyzer.getResult());
}
this.push(null);
cb();
} catch (err: any) {
cb(err);
}
this.push(null);
cb();
}
}

Expand Down

0 comments on commit 88658b9

Please sign in to comment.