Skip to content

Commit 829842a

Browse files
feat(utxo-bin): add nodeCatchError
Wrapper around `node()` and `handleParseError()` to catch errors and return a parse error node instead of throwing an error. Issue: BTC-1533
1 parent 55be38f commit 829842a

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

modules/utxo-bin/src/Parser.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ export type ParserNode = {
2323

2424
export class Parser {
2525
parseError: 'throw' | 'continue';
26+
2627
constructor(params: { parseError?: 'throw' | 'continue' } = {}) {
2728
this.parseError = params.parseError ?? 'continue';
2829
}
30+
2931
node(label: string | number, value: ParserNodeValue, nodes: ParserNode[] = []): ParserNode {
3032
if (!isParserNodeValue(value)) {
3133
throw new Error(`invalid node value ${typeof value}`);
@@ -38,6 +40,18 @@ export class Parser {
3840
};
3941
}
4042

43+
nodeCatchError(
44+
label: string | number,
45+
buildValue: () => ParserNodeValue | undefined,
46+
buildNodes: () => ParserNode[] = () => []
47+
): ParserNode {
48+
try {
49+
return this.node(label, buildValue?.(), buildNodes());
50+
} catch (e) {
51+
return this.handleParseError(e);
52+
}
53+
}
54+
4155
handleParseError(e: unknown): ParserNode {
4256
if (this.parseError === 'throw') {
4357
throw e;

0 commit comments

Comments
 (0)