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 catch spawnWorker() error outside #712

Open
cheese0409 opened this issue Feb 8, 2023 · 1 comment
Open

Cannot catch spawnWorker() error outside #712

cheese0409 opened this issue Feb 8, 2023 · 1 comment

Comments

@cheese0409
Copy link

cheese0409 commented Feb 8, 2023

Describe the bug
image
I am trying to use this great package to build a VS code extension, however it's weird that I cannot catch the error outside even if i wrapped try catch block outside or add catch block after the createWorker call.

To Reproduce
Steps to reproduce the behavior:

  1. can follow the link to create a hello-world example
  2. modified the activate function code as follows
export async function activate(context: vscode.ExtensionContext) {
  const disposable = vscode.commands.registerCommand(
    'test.helloWorld',
    async () => {
      try {
        console.log('before create worker')
        const worker = await tesseract.createWorker();
        console.log(worker); // this line cannot print in the debug console
        vscode.window.showInformationMessage('Hello World from test!');
      } catch (e) {
        console.log('error', e);
      }
    }
  );
  context.subscriptions.push(disposable);
}
  1. run debug this extension in the panel and type corresponding Hello World command in the test vscode window, then you can check the debug console you develop, it will print the logs

The logs only has 'before create worker', which means there is something wrong happens in the createWorker().
image

After the debugging, I found that it's because I didn't specify the workerPath which cause the spawnWorker() function failed. However, I cannot catch the bugs outside, could anyone helps to look at if I am doing a wrong catch way?

Expected behavior
Can catch the error outside.

@Balearica
Copy link
Collaborator

Balearica commented Aug 24, 2023

I was able to replicate what you describe, thanks for reporting this issue. I think this will require a patch to Tesseract.js to resolve. The core issue appears to be that the onerror function (line 42 in your screenshot) is being attached after the error occurs (line 40 in your screenshot).

This seems like something many other projects would encounter (and document a solution to) when working with workers, however upon a brief web search, I was unable to find any way to get the onerror function to trigger for an error during the creation of the worker. Perhaps some cleaner solution exists, but one idea is to send a "dummy" message to the worker immediately after creation, and throwing an error if no response is received after a set timeout.

This problem appears to be specific to Electron (which I believe VSCode uses), and does not occur in the browser. In addition to the fact that createWorker does not fail using default settings in the browser, if we run your code with an intentionally incorrect workerPath value, we can confirm that the catch block is entered.

      try {
        console.log('before create worker')
        const worker = await Tesseract.createWorker("eng", 1, {
          workerPath: "https://cdn.jsdelivr.net/npm/tesseract.js@5.0.2/dist/fake.min.js"
        });
        console.log('after create worker');
      } catch (e) {
        console.log('Inside catch block')
        console.log('error', e);
      }

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