From e32916cad3644e2fe8d07420445f882975f81412 Mon Sep 17 00:00:00 2001 From: Yashas Date: Tue, 25 Aug 2026 15:23:35 +0530 Subject: [PATCH] Read the extract response correctly, and fail visibly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit /ocr-extract answers { extracted: {...}, raw: "..." }. The client checked `fields` first and `extracted` second, which was right by luck — but an extraction that returned nothing, or threw, did so silently: the button went back to idle and the operator had no way to tell a read from a no-read. Now an empty or failed extraction says so under the field, and either way the UPLOAD survives. The file is already stored and referenced; a failed read only means the fields are not pre-filled, which someone can recover by typing. Losing the upload because the read failed would not be recoverable. Co-Authored-By: Claude Opus 5 (1M context) --- src/components/FileField.jsx | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/components/FileField.jsx b/src/components/FileField.jsx index 2ba0a8a..a640da9 100644 --- a/src/components/FileField.jsx +++ b/src/components/FileField.jsx @@ -36,11 +36,22 @@ export default function FileField({ field, value, instanceId, activityUid, onCha if (isOcr) { setBusy('Reading the document…') - const out = await client.ocrExtract(ref, ctx) - const fields = out?.fields ?? out?.extracted ?? out?.data ?? out - if (fields && typeof fields === 'object') { - setExtracted(fields) - onExtract?.(fields) + // Extraction failing must NOT lose the upload. The file is already + // stored and referenced; a failed read just means the fields are not + // pre-filled, which is recoverable by typing. Swallowing the upload + // because the OCR errored would not be. + try { + const out = await client.ocrExtract(ref, ctx) + // The endpoint answers { extracted: {...}, raw: "..." }. + const fields = out?.extracted ?? out?.fields ?? out?.data ?? null + if (fields && typeof fields === 'object' && Object.keys(fields).length) { + setExtracted(fields) + onExtract?.(fields) + } else { + setErr({ status: '', message: 'Uploaded, but nothing could be read from this document.' }) + } + } catch (ox) { + setErr({ status: ox.status ?? '', message: 'Uploaded, but reading it failed — ' + (ox.message || 'unknown error') }) } } } catch (ex) {