Read the extract response correctly, and fail visibly

/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) <noreply@anthropic.com>
This commit is contained in:
Yashas 2026-08-25 15:23:35 +05:30
parent 43259b378c
commit e32916cad3

View File

@ -36,11 +36,22 @@ export default function FileField({ field, value, instanceId, activityUid, onCha
if (isOcr) { if (isOcr) {
setBusy('Reading the document…') setBusy('Reading the document…')
const out = await client.ocrExtract(ref, ctx) // Extraction failing must NOT lose the upload. The file is already
const fields = out?.fields ?? out?.extracted ?? out?.data ?? out // stored and referenced; a failed read just means the fields are not
if (fields && typeof fields === 'object') { // pre-filled, which is recoverable by typing. Swallowing the upload
setExtracted(fields) // because the OCR errored would not be.
onExtract?.(fields) 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) { } catch (ex) {