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

How to handle errors from the BrowserMultiFormatReader? #624

Open
Laurenz-M opened this issue Oct 28, 2024 · 1 comment
Open

How to handle errors from the BrowserMultiFormatReader? #624

Laurenz-M opened this issue Oct 28, 2024 · 1 comment
Labels

Comments

@Laurenz-M
Copy link

I'm having trouble to receive any errors or results in the callback of "decodeFromVideoDevice".
This is a simplification of my code:
const reader = new BrowserMultiFormatReader(); const deviceId = '[valid device id here]' reader.decodeFromVideoDevice(deviceId, 'video', (result, err) => { console.log('test', err); if (!result) return; })
The whole code works, when the camera does not emit any errors and i can successfully scan items.

My issue is with the error-handling, as the console log with "test" does never trigger, when there is an error.
To test this, I triggered errors for "NotAllowedError: Permission denied", "NotAllowedError: Permission dismissed" and "NotReadableError: Device in use", which are all correctly recognized and logged in the console by the library code itself, but are not returned in the error callback.

My understanding is that the errors happen before the actual initialization and are this not returned.

It would be great if someone could assist me in this matter.
Thanks a lot! :)
Screenshot 2024-10-28 120909

@renenm
Copy link

renenm commented Jan 28, 2025

Hi Laurenz, the catch and callback actually serve different purposes:
The callback is called for each scan attempt and handles scan-related errors. The catch handles errors during the initial camera setup (e.g., when the camera is already in use)

Here's an example of how to implement both correctly:

const callback = (result, err) => {
    if (result) {
        console.log('Scan successful:', result.getText());
    }
    if (err && !(err instanceof NotFoundException)) {
        console.error('Scanning error:', err);
    }
};

codeReader
    .decodeFromVideoDevice(selectedDeviceId, 'video', callback)
    .catch((error) => {
        console.error('Camera initialization error:', error);
        if (error.name === 'NotReadableError') {
            // Handle camera being in use
        }
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants