Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split the eror-raising part of constructor into separate method. #35

Closed
wants to merge 2 commits into from

Conversation

x2nie
Copy link

@x2nie x2nie commented Oct 23, 2024

Hi !
I found this lib is quite good, but I face a trouble while inherit the Parser class.

My situation is: I want to parse a section of xml (a string, not a whole file), hence it may has several root elements.
Now I want my (inheritance) parser to keep parsing for second root and so.
Ideally, it would be like as:

class MyParser extends Parser {
    //? Allow multiroot
    constructor(xml: string, options: ParserOptions = {}) {
        try {
            super(xml, options);
        } catch (err) {
            // we do not care about multiroot validation here
            if(!(err as Error).message.startsWith('Extra content at the end of the document'))
                throw err; // show non multi-root error
        
            while (!this.scanner.isEnd) {
                this.consumeElement();
                this.consumeMisc();
            }
        }
    }

But that code is invalid; bacause super() can't be called inside try..catch.

Currently, I solve it by copying the whole class and modify the constructor; and yes it works, but it isn't what I want to do.
Modifying some methods (as needed) is the best practice, I think.

So, here the patch to allow the parser to be inherited (especially to not throwing error on constructor).

note:

  • I am sorry for my bad english I wish you okay.
  • I also pass the tests, and its fine too.
  • here a picture below: multi root xml usage in real life (my VS Code notebook project).

image

Copy link
Owner

@rgrove rgrove left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This seems reasonable. I should have time to merge it soon.

It looks like I'll need to update the GitHub Actions workflow to fix some problems; the failures there aren't related to this change.

One thing to be aware of if you're extending the Parser class is that this class isn't part of parse-xml's public API, so it may experience breaking changes in minor and patch releases.

@rgrove
Copy link
Owner

rgrove commented Oct 25, 2024

Merged! I renamed the startParse method to parse to be a little less verbose. This will be released in v4.2.0.

@rgrove rgrove closed this Oct 25, 2024
@x2nie x2nie deleted the temp-alwaysfine-constructor branch October 28, 2024 04:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants