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

Cannot read properties of undefined (reading 'terminate') #19

Open
YOUR1 opened this issue Aug 30, 2023 · 4 comments
Open

Cannot read properties of undefined (reading 'terminate') #19

YOUR1 opened this issue Aug 30, 2023 · 4 comments

Comments

@YOUR1
Copy link

YOUR1 commented Aug 30, 2023

Whenever I try the browser-version with a simple class:

import * as xmllint from "xmllint-wasm/index-browser";

export class ModelValidator {
    constructor(private xml: string) {}

    private async getSchema(): Promise<string> {
        const response = await fetch(Model.xsd');
        return await response.text()
    }

    async process(callback: any) {
        xmllint.memoryPages
        const { valid, rawOutput, normalized } = await xmllint.validateXML({
            xml: { fileName: 'model.xml', contents: this.xml },
            schema: await this.getSchema(),
            normalization: 'format'
        });
        callback(valid, rawOutput, normalized);
    }

}

I get the following error:

ModelValidator.ts:9 ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'terminate')
TypeError: Cannot read properties of undefined (reading 'terminate')
    at index-browser.mjs:165:26
    at _ZoneDelegate.invoke (zone.js:368:26)
    at Object.onInvoke (core.mjs:27450:33)
    at _ZoneDelegate.invoke (zone.js:367:52)
    at Zone.run (zone.js:129:43)
    at zone.js:1257:36
    at _ZoneDelegate.invokeTask (zone.js:402:31)
    at core.mjs:27127:55
    at AsyncStackTaggingZoneSpec.onInvokeTask (core.mjs:27127:36)
    at _ZoneDelegate.invokeTask (zone.js:401:60)
    at resolvePromise (zone.js:1193:31)
    at zone.js:1100:17
    at zone.js:1116:33
    at asyncGeneratorStep (asyncToGenerator.js:6:1)
    at _throw (asyncToGenerator.js:25:1)
    at _ZoneDelegate.invoke (zone.js:368:26)
    at Object.onInvoke (core.mjs:27450:33)
    at _ZoneDelegate.invoke (zone.js:367:52)
    at Zone.run (zone.js:129:43)
    at zone.js:1257:36

Is there something I need to do additionally?

@noppa
Copy link
Owner

noppa commented Aug 30, 2023

What kind of bundler/build setup are you using? The stack trace implies that there may be some stuff going on that isn't coming from this lib, but probably zone.js. Sooo... Angular? What does your ng version (or npx ng version or something) say?

@noppa
Copy link
Owner

noppa commented Aug 30, 2023

The only way that I can think of that this could happen is that if this line in index.js throws

worker = new Worker(new URL('./xmllint-browser.mjs', import.meta.url), { type: 'module' });

Then worker will be undefined, and I guess I have a bit of an error handling bug in there that we kinda swallow the original error when we fail to terminate the never-created worker in finally.

Until the error handling problem is fixed, I recommend you open the index-browser.mjs file under node_modules, and try removing this line

.finally(() => worker.terminate());

That won't fix the actual issue, but it will fix the poor error message and might give us better hint on why the worker can't be initialized.

@YOUR1
Copy link
Author

YOUR1 commented Aug 30, 2023

What kind of bundler/build setup are you using? The stack trace implies that there may be some stuff going on that isn't coming from this lib, but probably zone.js. Sooo... Angular? What does your ng version (or npx ng version or something) say?

Yeah, indeed. I'm using Angular (16.2.1);

noppa added a commit that referenced this issue Sep 30, 2023
Related to #19, this "fixes" an unactionable global uncaught error in
case the xmllint worker could not be initialized for whatever reason.

This patch should not cause runtime application code differences,
because the error that this case created would've already been swallowed
by the earlier error (as per how Promise.finally works on rejected
Promise), but it did create a confusing error to logs / global error
handler.
@noppa
Copy link
Owner

noppa commented Sep 30, 2023

I have fixed the confusing Cannot read properties of undefined error on version 4.0.2. This won't fix your actual issue, because for some reason or another the code running in your environment is not able to do new Worker(...). But at least now it's easier to find the original error and focus on solving the actual issue, rather than an unrelated error that is more of an effect rather than the cause.

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

No branches or pull requests

2 participants