From 39248a560baf2c7030c3c3ecc7adecc00c93d0b9 Mon Sep 17 00:00:00 2001 From: Kwangsoo Yeo Date: Thu, 26 Oct 2023 14:13:34 -0700 Subject: [PATCH] web error handler (#186) --- binding/web/package.json | 2 +- binding/web/src/cobra_worker.ts | 6 ++++++ binding/web/src/cobra_worker_handler.ts | 21 +++++++++++++++------ demo/web/package.json | 2 +- demo/web/yarn.lock | 8 ++++---- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/binding/web/package.json b/binding/web/package.json index 853947fa..24cecc4e 100644 --- a/binding/web/package.json +++ b/binding/web/package.json @@ -3,7 +3,7 @@ "description": "Cobra VAD engine for web browsers (via WebAssembly)", "author": "Picovoice Inc", "license": "Apache-2.0", - "version": "2.0.0", + "version": "2.0.1", "keywords": [ "cobra", "web", diff --git a/binding/web/src/cobra_worker.ts b/binding/web/src/cobra_worker.ts index e3941c4f..615ed3f5 100644 --- a/binding/web/src/cobra_worker.ts +++ b/binding/web/src/cobra_worker.ts @@ -26,6 +26,7 @@ export class CobraWorker { private readonly _version: string; private readonly _frameLength: number; private readonly _sampleRate: number; + private static _sdk: string = 'web'; private static _wasm: string; private static _wasmSimd: string; @@ -90,6 +91,10 @@ export class CobraWorker { } } + public static setSdk(sdk: string): void { + CobraWorker._sdk = sdk; + } + /** * Creates a worker instance of the Picovoice Cobra VAD engine. * Behind the scenes, it requires the WebAssembly code to load and initialize before @@ -168,6 +173,7 @@ export class CobraWorker { accessKey: accessKey, wasm: this._wasm, wasmSimd: this._wasmSimd, + sdk: this._sdk, options: workerOptions, }); diff --git a/binding/web/src/cobra_worker_handler.ts b/binding/web/src/cobra_worker_handler.ts index 60f7b613..8601a44e 100644 --- a/binding/web/src/cobra_worker_handler.ts +++ b/binding/web/src/cobra_worker_handler.ts @@ -55,6 +55,7 @@ self.onmessage = async function ( Cobra.setWasm(event.data.wasm); Cobra.setWasmSimd(event.data.wasmSimd); + Cobra.setSdk(event.data.sdk); cobra = await Cobra.create( event.data.accessKey, voiceProbabilityCallback, @@ -68,12 +69,20 @@ self.onmessage = async function ( sampleRate: cobra.sampleRate, }); } catch (e: any) { - self.postMessage({ - command: 'error', - status: PvStatus.RUNTIME_ERROR, - shortMessage: e.shortMessage, - messageStack: e.messageStack - }); + if (e instanceof CobraError) { + self.postMessage({ + command: 'error', + status: e.status, + shortMessage: e.shortMessage, + messageStack: e.messageStack + }); + } else { + self.postMessage({ + command: 'error', + status: PvStatus.RUNTIME_ERROR, + shortMessage: e.message + }); + } } break; case 'process': diff --git a/demo/web/package.json b/demo/web/package.json index b5132fde..54821ffa 100644 --- a/demo/web/package.json +++ b/demo/web/package.json @@ -17,7 +17,7 @@ "author": "Picovoice Inc", "license": "Apache-2.0", "dependencies": { - "@picovoice/cobra-web": "~2.0.0", + "@picovoice/cobra-web": "~2.0.1", "@picovoice/web-voice-processor": "~4.0.8" }, "devDependencies": { diff --git a/demo/web/yarn.lock b/demo/web/yarn.lock index d6f9bf7a..87474f8d 100644 --- a/demo/web/yarn.lock +++ b/demo/web/yarn.lock @@ -2,10 +2,10 @@ # yarn lockfile v1 -"@picovoice/cobra-web@~2.0.0": - version "2.0.0" - resolved "https://registry.yarnpkg.com/@picovoice/cobra-web/-/cobra-web-2.0.0.tgz#fae10248546072faba3e784288135b07e576b0cd" - integrity sha512-twgDJKkty+NXUG9/1feM15TLTvyCTjiDLwoncWFeHqcJ02peIa84T27/5atzpYkXKKA3sp+/B1a+6EKDq7P6/g== +"@picovoice/cobra-web@~2.0.1": + version "2.0.1" + resolved "https://registry.yarnpkg.com/@picovoice/cobra-web/-/cobra-web-2.0.1.tgz#b2235af36439c23c192957abfed1b329c9a7b7a5" + integrity sha512-2ealbr5Km7NtvuAdTfQyWMI7DvHoi+A4wRnZx1JHRFp4NutfPwCO/xCmxmh2neiya4J1L6tpEuOafputdAfS/w== dependencies: "@picovoice/web-utils" "=1.3.1"