fix(ocr): errors not properly shown due to lack of convention

This commit is contained in:
Elian Doran
2026-04-01 16:58:34 +03:00
parent 6d050340ee
commit 6534beec14
2 changed files with 26 additions and 27 deletions

View File

@@ -1,8 +1,8 @@
import { useEffect, useState } from "preact/hooks";
import { t } from "../../services/i18n";
import server from "../../services/server";
import toast from "../../services/toast";
import { t } from "../../services/i18n";
import { TypeWidgetProps } from "./type_widget";
interface TextRepresentationResponse {
@@ -10,7 +10,7 @@ interface TextRepresentationResponse {
text: string;
hasOcr: boolean;
extractedAt: string | null;
error?: string;
message?: string;
}
type State =
@@ -30,7 +30,7 @@ export default function ReadOnlyTextRepresentation({ note }: TypeWidgetProps) {
const response = await server.get<TextRepresentationResponse>(`ocr/notes/${note.noteId}/text`);
if (!response.success) {
setState({ kind: "error", message: response.error || t("ocr.failed_to_load") });
setState({ kind: "error", message: response.message || t("ocr.failed_to_load") });
return;
}
@@ -51,16 +51,15 @@ export default function ReadOnlyTextRepresentation({ note }: TypeWidgetProps) {
async function processOCR() {
setProcessing(true);
try {
const response = await server.post<{ success: boolean; error?: string }>(`ocr/process-note/${note.noteId}`);
const response = await server.post<{ success: boolean; message?: string }>(`ocr/process-note/${note.noteId}`);
if (response.success) {
toast.showMessage(t("ocr.processing_started"));
setTimeout(fetchText, 2000);
} else {
throw new Error(response.error || t("ocr.processing_failed"));
toast.showError(response.message || t("ocr.processing_failed"));
}
} catch (error: any) {
console.error("Error processing OCR:", error);
toast.showError(error.message || t("ocr.processing_failed"));
} catch {
// Server errors (4xx/5xx) are already shown as toasts by server.ts.
} finally {
setProcessing(false);
}

View File

@@ -71,7 +71,7 @@ async function processNoteOCR(req: Request<{ noteId: string }>, res: Response) {
if (!noteId) {
res.status(400).json({
success: false,
error: 'Note ID is required'
message: 'Note ID is required'
});
(res as any).triliumResponseHandled = true;
return;
@@ -81,7 +81,7 @@ async function processNoteOCR(req: Request<{ noteId: string }>, res: Response) {
if (!ocrService.isOCREnabled()) {
res.status(400).json({
success: false,
error: 'OCR is not enabled in settings'
message: 'OCR is not enabled in settings'
});
(res as any).triliumResponseHandled = true;
return;
@@ -92,7 +92,7 @@ async function processNoteOCR(req: Request<{ noteId: string }>, res: Response) {
if (!note) {
res.status(404).json({
success: false,
error: 'Note not found'
message: 'Note not found'
});
(res as any).triliumResponseHandled = true;
return;
@@ -106,7 +106,7 @@ async function processNoteOCR(req: Request<{ noteId: string }>, res: Response) {
if (!result) {
res.status(400).json({
success: false,
error: 'Note is not an image or has unsupported format'
message: 'Note is not an image or has unsupported format'
});
(res as any).triliumResponseHandled = true;
return;
@@ -122,7 +122,7 @@ async function processNoteOCR(req: Request<{ noteId: string }>, res: Response) {
log.error(`Error processing OCR for note: ${error instanceof Error ? error.message : String(error)}`);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : String(error)
message: error instanceof Error ? error.message : String(error)
});
(res as any).triliumResponseHandled = true;
}
@@ -177,7 +177,7 @@ async function processAttachmentOCR(req: Request<{ attachmentId: string }>, res:
if (!attachmentId) {
res.status(400).json({
success: false,
error: 'Attachment ID is required'
message: 'Attachment ID is required'
});
(res as any).triliumResponseHandled = true;
return;
@@ -187,7 +187,7 @@ async function processAttachmentOCR(req: Request<{ attachmentId: string }>, res:
if (!ocrService.isOCREnabled()) {
res.status(400).json({
success: false,
error: 'OCR is not enabled in settings'
message: 'OCR is not enabled in settings'
});
(res as any).triliumResponseHandled = true;
return;
@@ -198,7 +198,7 @@ async function processAttachmentOCR(req: Request<{ attachmentId: string }>, res:
if (!attachment) {
res.status(404).json({
success: false,
error: 'Attachment not found'
message: 'Attachment not found'
});
(res as any).triliumResponseHandled = true;
return;
@@ -212,7 +212,7 @@ async function processAttachmentOCR(req: Request<{ attachmentId: string }>, res:
if (!result) {
res.status(400).json({
success: false,
error: 'Attachment is not an image or has unsupported format'
message: 'Attachment is not an image or has unsupported format'
});
(res as any).triliumResponseHandled = true;
return;
@@ -228,7 +228,7 @@ async function processAttachmentOCR(req: Request<{ attachmentId: string }>, res:
log.error(`Error processing OCR for attachment: ${error instanceof Error ? error.message : String(error)}`);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : String(error)
message: error instanceof Error ? error.message : String(error)
});
(res as any).triliumResponseHandled = true;
}
@@ -281,7 +281,7 @@ async function searchOCR(req: Request, res: Response) {
if (!searchText || typeof searchText !== 'string') {
res.status(400).json({
success: false,
error: 'Search query is required'
message: 'Search query is required'
});
(res as any).triliumResponseHandled = true;
return;
@@ -299,7 +299,7 @@ async function searchOCR(req: Request, res: Response) {
log.error(`Error searching OCR results: ${error instanceof Error ? error.message : String(error)}`);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : String(error)
message: error instanceof Error ? error.message : String(error)
});
(res as any).triliumResponseHandled = true;
}
@@ -347,7 +347,7 @@ async function batchProcessOCR(req: Request, res: Response) {
log.error(`Error initiating batch OCR processing: ${error instanceof Error ? error.message : String(error)}`);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : String(error)
message: error instanceof Error ? error.message : String(error)
});
(res as any).triliumResponseHandled = true;
}
@@ -391,7 +391,7 @@ async function getBatchProgress(req: Request, res: Response) {
} catch (error: unknown) {
log.error(`Error getting batch OCR progress: ${error instanceof Error ? error.message : String(error)}`);
res.status(500).json({
error: error instanceof Error ? error.message : String(error)
message: error instanceof Error ? error.message : String(error)
});
(res as any).triliumResponseHandled = true;
}
@@ -442,7 +442,7 @@ async function getOCRStats(req: Request, res: Response) {
log.error(`Error getting OCR stats: ${error instanceof Error ? error.message : String(error)}`);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : String(error)
message: error instanceof Error ? error.message : String(error)
});
(res as any).triliumResponseHandled = true;
}
@@ -488,7 +488,7 @@ async function deleteOCRResults(req: Request<{ blobId: string }>, res: Response)
if (!blobId) {
res.status(400).json({
success: false,
error: 'Blob ID is required'
message: 'Blob ID is required'
});
(res as any).triliumResponseHandled = true;
return;
@@ -506,7 +506,7 @@ async function deleteOCRResults(req: Request<{ blobId: string }>, res: Response)
log.error(`Error deleting OCR results: ${error instanceof Error ? error.message : String(error)}`);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : String(error)
message: error instanceof Error ? error.message : String(error)
});
(res as any).triliumResponseHandled = true;
}
@@ -557,7 +557,7 @@ async function getNoteOCRText(req: Request<{ noteId: string }>, res: Response) {
if (!note) {
res.status(404).json({
success: false,
error: 'Note not found'
message: 'Note not found'
});
(res as any).triliumResponseHandled = true;
return;
@@ -594,7 +594,7 @@ async function getNoteOCRText(req: Request<{ noteId: string }>, res: Response) {
log.error(`Error getting OCR text for note: ${error instanceof Error ? error.message : String(error)}`);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
message: error instanceof Error ? error.message : 'Unknown error'
});
(res as any).triliumResponseHandled = true;
}