From 3e3d4e3c3e4671043a2f00b2d5dfebf18ed62bbf Mon Sep 17 00:00:00 2001 From: Jappie Klooster Date: Mon, 1 Apr 2024 15:14:06 +0200 Subject: [PATCH 1/4] Expose error callbacks, prevent always setting header this allows debugging our local app these weird errors appearing in the header. it also allows us to send the errors to rollbar instead of informing the user. Which is usefull for development, but kinda terrible for production systems. fix webpack build script --- scripts/build-webpack.sh | 2 +- src/core.ts | 3 +++ src/html5-qrcode-scanner.ts | 25 ++++++++++++++++++------- src/html5-qrcode.ts | 2 +- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/scripts/build-webpack.sh b/scripts/build-webpack.sh index 9fde2fa9..0267c809 100755 --- a/scripts/build-webpack.sh +++ b/scripts/build-webpack.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash ## Build Script echo 'Initiating webpack build sequence.' diff --git a/src/core.ts b/src/core.ts index 8d3d9656..c4ee39cf 100644 --- a/src/core.ts +++ b/src/core.ts @@ -250,6 +250,9 @@ export type QrcodeSuccessCallback export type QrcodeErrorCallback = (errorMessage: string, error: Html5QrcodeError) => void; +export type StartErrorCallback + = (error: Html5QrcodeError) => void; + /** Code decoder interface. */ export interface QrcodeDecoderAsync { /** diff --git a/src/html5-qrcode-scanner.ts b/src/html5-qrcode-scanner.ts index 028262fe..50c42563 100644 --- a/src/html5-qrcode-scanner.ts +++ b/src/html5-qrcode-scanner.ts @@ -12,6 +12,7 @@ import { Html5QrcodeConstants, Html5QrcodeScanType, QrcodeSuccessCallback, + StartErrorCallback, QrcodeErrorCallback, Html5QrcodeResult, Html5QrcodeError, @@ -196,6 +197,9 @@ export class Html5QrcodeScanner { private cameraScanImage: HTMLImageElement | null = null; private fileScanImage: HTMLImageElement | null = null; private fileSelectionUi: FileSelectionUi | null = null; + + public startErrorCallback: StartErrorCallback; + public torchButtonErrorCallback: StartErrorCallback; //#endregion /** @@ -228,6 +232,15 @@ export class Html5QrcodeScanner { if (config!.rememberLastUsedCamera !== true) { this.persistedDataManager.reset(); } + const $this = this; + + const warnUserViaHeader : StartErrorCallback = (error) => { + $this.setHeaderMessage( + error.toString(), Html5QrcodeScannerStatus.STATUS_WARNING); + }; + + this.startErrorCallback = warnUserViaHeader; + this.torchButtonErrorCallback = warnUserViaHeader; } /** @@ -584,8 +597,7 @@ export class Html5QrcodeScanner { // time. createPermissionButtonIfNotExists(); } - $this.setHeaderMessage( - error, Html5QrcodeScannerStatus.STATUS_WARNING); + $this.startErrorCallback(error); $this.showHideScanTypeSwapLink(true); }); } @@ -765,6 +777,7 @@ export class Html5QrcodeScanner { // Optional torch button support. let torchButton: TorchButton; + const createAndShowTorchButtonIfSupported = (cameraCapabilities: CameraCapabilities) => { if (!cameraCapabilities.torchFeature().isSupported()) { @@ -782,9 +795,7 @@ export class Html5QrcodeScanner { { display: "none", marginLeft: "5px" }, // Callback in case of torch action failure. (errorMessage) => { - $this.setHeaderMessage( - errorMessage, - Html5QrcodeScannerStatus.STATUS_WARNING); + $this.torchButtonErrorCallback(Html5QrcodeErrorFactory.createFrom(errorMessage)) } ); } else { @@ -996,13 +1007,13 @@ export class Html5QrcodeScanner { } } - private resetHeaderMessage() { + public resetHeaderMessage() { const messageDiv = document.getElementById( this.getHeaderMessageContainerId())!; messageDiv.style.display = "none"; } - private setHeaderMessage( + public setHeaderMessage( messageText: string, scannerStatus?: Html5QrcodeScannerStatus) { if (!scannerStatus) { scannerStatus = Html5QrcodeScannerStatus.STATUS_DEFAULT; diff --git a/src/html5-qrcode.ts b/src/html5-qrcode.ts index b3fcbdab..0a4ec8de 100644 --- a/src/html5-qrcode.ts +++ b/src/html5-qrcode.ts @@ -363,7 +363,7 @@ export class Html5Qrcode { ): Promise { // Code will be consumed as javascript. - if (!cameraIdOrConfig) { + if (!cameraIdOrConfig) { throw "cameraIdOrConfig is required"; } From 22d62f55ef685bd97d3659c47e3bf04bac35c71c Mon Sep 17 00:00:00 2001 From: Jappie Klooster Date: Mon, 1 Apr 2024 20:42:17 +0200 Subject: [PATCH 2/4] fix ci? --- src/html5-qrcode-scanner.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/html5-qrcode-scanner.ts b/src/html5-qrcode-scanner.ts index 50c42563..0431b73b 100644 --- a/src/html5-qrcode-scanner.ts +++ b/src/html5-qrcode-scanner.ts @@ -795,7 +795,7 @@ export class Html5QrcodeScanner { { display: "none", marginLeft: "5px" }, // Callback in case of torch action failure. (errorMessage) => { - $this.torchButtonErrorCallback(Html5QrcodeErrorFactory.createFrom(errorMessage)) + $this.torchButtonErrorCallback(Html5QrcodeErrorFactory.createFrom(errorMessage)); } ); } else { @@ -1013,6 +1013,7 @@ export class Html5QrcodeScanner { messageDiv.style.display = "none"; } + /*eslint complexity: ["error", 5]*/ public setHeaderMessage( messageText: string, scannerStatus?: Html5QrcodeScannerStatus) { if (!scannerStatus) { From bf57be23ad58b8d734507b441894aa0c81c88cd6 Mon Sep 17 00:00:00 2001 From: Jappie Klooster Date: Tue, 2 Apr 2024 12:22:19 +0200 Subject: [PATCH 3/4] add more error callbacks --- src/html5-qrcode-scanner.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/html5-qrcode-scanner.ts b/src/html5-qrcode-scanner.ts index 0431b73b..ee3fb488 100644 --- a/src/html5-qrcode-scanner.ts +++ b/src/html5-qrcode-scanner.ts @@ -200,6 +200,8 @@ export class Html5QrcodeScanner { public startErrorCallback: StartErrorCallback; public torchButtonErrorCallback: StartErrorCallback; + public getCamaraErrorCallback: StartErrorCallback; + public clickListenerErrorCallback: StartErrorCallback; //#endregion /** @@ -241,6 +243,8 @@ export class Html5QrcodeScanner { this.startErrorCallback = warnUserViaHeader; this.torchButtonErrorCallback = warnUserViaHeader; + this.getCamaraErrorCallback = warnUserViaHeader; + this.clickListenerErrorCallback = warnUserViaHeader; } /** @@ -597,7 +601,7 @@ export class Html5QrcodeScanner { // time. createPermissionButtonIfNotExists(); } - $this.startErrorCallback(error); + $this.getCamaraErrorCallback(error); $this.showHideScanTypeSwapLink(true); }); } @@ -864,8 +868,7 @@ export class Html5QrcodeScanner { $this.showHideScanTypeSwapLink(true); cameraSelectUi.enable(); resetCameraActionStartButton(/* shouldShow= */ true); - $this.setHeaderMessage( - error, Html5QrcodeScannerStatus.STATUS_WARNING); + $this.startErrorCallback(error); }); }); @@ -901,8 +904,7 @@ export class Html5QrcodeScanner { $this.insertCameraScanImageToScanRegion(); }).catch((error) => { cameraActionStopButton.disabled = false; - $this.setHeaderMessage( - error, Html5QrcodeScannerStatus.STATUS_WARNING); + $this.clickListenerErrorCallback(error); }); }); From a667b1313a0ab86d3394d434c801358599f6fd2e Mon Sep 17 00:00:00 2001 From: Jappie Klooster Date: Thu, 4 Apr 2024 14:18:24 +0200 Subject: [PATCH 4/4] undo unrelated changes --- scripts/build-webpack.sh | 2 +- src/html5-qrcode.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/build-webpack.sh b/scripts/build-webpack.sh index 0267c809..9fde2fa9 100755 --- a/scripts/build-webpack.sh +++ b/scripts/build-webpack.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/bash ## Build Script echo 'Initiating webpack build sequence.' diff --git a/src/html5-qrcode.ts b/src/html5-qrcode.ts index 0a4ec8de..b3fcbdab 100644 --- a/src/html5-qrcode.ts +++ b/src/html5-qrcode.ts @@ -363,7 +363,7 @@ export class Html5Qrcode { ): Promise { // Code will be consumed as javascript. - if (!cameraIdOrConfig) { + if (!cameraIdOrConfig) { throw "cameraIdOrConfig is required"; }