feat: custom parser error handler (#222)

As suggested in issue #218
This commit is contained in:
Daniel Wykerd
2022-10-18 23:44:22 +02:00
committed by GitHub
parent d71b762df5
commit 8740deb1f2

View File

@@ -170,9 +170,17 @@ export class LiveChatContinuation extends YTNode {
}
}
export type ParserError = { classname: string, classdata: any, err: any };
export type ParserErrorHandler = (error: ParserError) => void;
export default class Parser {
static #errorHandler: ParserErrorHandler = Parser.#printError;
static #memo: Memo | null = null;
static setParserErrorHandler(handler: ParserErrorHandler) {
this.#errorHandler = handler;
}
static #clearMemo() {
Parser.#memo = null;
}
@@ -356,7 +364,7 @@ export default class Parser {
return result as T;
} catch (err) {
this.printError({ classname, classdata: data[keys[0]], err });
this.#errorHandler({ classname, classdata: data[keys[0]], err });
return null;
}
}
@@ -445,7 +453,7 @@ export default class Parser {
}
}
static printError({ classname, classdata, err }: { classname: string, classdata: any, err: any }) {
static #printError({ classname, classdata, err }: ParserError) {
if (err.code == 'MODULE_NOT_FOUND') {
return console.warn(
new InnertubeError(