Handling errors with the Web Barcode Scanner SDK
Opening a scanning screen might fail for various reasons, e.g., an unsupported browser, permissions were denied, the camera does not exist, etc.
The Scanbot SDK attempts to handle these cases as best as possible, however, as hardware access API in the browser is not backwards compatible or future-proof, this is an ongoing challenge. To prevent false positives, we keep error handling to a minimum.
If the user has denied camera permission, the Promise for creating a scanner (createDocumentScanner,
createBarcodeScanner, createMrzScanner, etc.), will be rejected with MediaPermissionError.
If it is an unsupported browser (specifically, mediaDevices are not available), the Promise will be rejected with UnsupportedMediaDevicesError.
If the matching media is not available, the Promise will be rejected with MediaNotAvailableError.
The Promise will be rejected with LicenseError when a client calls any of the Scanbot SDK functions while having a license error.
If the encountered error is not one of the errors above and not specified in scanner specific documentation pages, it will be rejected with UnknownError.
Promise rejections means you should try and catch when starting the scanners. If a scanner throws an error while starting (e.g., due to a missing camera permission), the error can be caught as follows:
try {
barcodeScanner = await scanbotSDK.createBarcodeScanner(config);
} catch (error) {
// Handle cases where scanner could not be opened
console.log(error.name) // Console log may be UnsupportedMediaDevicesError, MediaPermissionError, ...
}
If a scanner throws an error after being successfully started, it can be caught via an onError callback in the configuration:
const config = {
containerId: containerId,
onError: (error) => {
// Action taken
},
...
};
There is no internal and continuous check for invalid or expired license. You should always check the license this yourself via await scanbotSDK.getLicenseInfo().
Want to scan longer than one minute?
Generate a free trial license to test the Scanbot SDK thoroughly.
Get free trial license