From 4ac3036000902f72cb33c148159cdc93168cbe55 Mon Sep 17 00:00:00 2001 From: ztimson Date: Tue, 9 Jun 2026 09:41:09 -0400 Subject: [PATCH] Proper error handling for OCR --- package.json | 2 +- src/vision.ts | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index c930397..7fd2557 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/ai-utils", - "version": "1.0.4", + "version": "1.0.5", "description": "AI Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/vision.ts b/src/vision.ts index ab617eb..895094b 100644 --- a/src/vision.ts +++ b/src/vision.ts @@ -14,9 +14,13 @@ export class Vision { let worker: any; const p = (async () => { worker = await createWorker(this.ai.options.ocr || 'eng', 2, {cachePath: this.ai.options.path}); - const {data} = await worker.recognize(path); - await worker.terminate(); - return data.text.trim() || null; + worker.setParameters({}).catch(() => {}); // force error handler attachment + return await new Promise((res, rej) => { + worker.on?.('error', rej); // catch worker-level throws + worker.recognize(path) + .then(({data}: any) => res(data.text.trim() || null)) + .catch(rej); + }); })().finally(() => worker?.terminate()); return Object.assign(p, {abort: () => worker?.terminate()}); }