Skip to content

Commit f2bd6eb

Browse files
committed
fix: Enhance error handling and add debug message support in report processing
1 parent 8396993 commit f2bd6eb

File tree

3 files changed

+61
-21
lines changed

3 files changed

+61
-21
lines changed

backend/src/document-processor/controllers/document-processor.controller.ts

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,41 @@ export class DocumentProcessorController {
182182
this.logger.log(`Started async processing for report: ${reportId}`);
183183

184184
// Get the file from S3
185-
const fileBuffer = await this.getFileFromS3(filePath);
185+
let fileBuffer;
186+
try {
187+
fileBuffer = await this.getFileFromS3(filePath);
188+
this.logger.log(`Successfully retrieved file from S3 for report: ${reportId}`);
189+
} catch (error) {
190+
this.logger.error(
191+
`Failed to retrieve file from S3 for report ${reportId}: ${error instanceof Error ? error.message : 'Unknown error'}`,
192+
);
193+
await this.updateReportStatus(reportId, userId, ProcessingStatus.FAILED);
194+
return;
195+
}
186196

187197
// Process the document
188-
const result = await this.documentProcessorService.processDocument(fileBuffer, userId);
198+
let result;
199+
try {
200+
result = await this.documentProcessorService.processDocument(fileBuffer, userId);
201+
this.logger.log(`Successfully processed document for report: ${reportId}`);
202+
} catch (error) {
203+
this.logger.error(
204+
`Failed to process document for report ${reportId}: ${error instanceof Error ? error.message : 'Unknown error'}`,
205+
);
206+
await this.updateReportStatus(reportId, userId, ProcessingStatus.FAILED);
207+
return;
208+
}
189209

190210
// Fetch the report again to ensure we have the latest version
191211
const report = await this.reportsService.findOne(reportId, userId);
192212
if (!report) {
193-
throw new Error(`Report ${reportId} not found during async processing`);
213+
this.logger.error(`Report ${reportId} not found during async processing`);
214+
return;
194215
}
195216

196217
// Update the report with analysis results
197-
report.title = result.analysis.title || 'Untitled Report';
198-
report.category = result.analysis.category || 'general';
218+
report.title = result.analysis.title;
219+
report.category = result.analysis.category;
199220
report.processingStatus = ProcessingStatus.PROCESSED;
200221

201222
// Extract lab values
@@ -214,25 +235,40 @@ export class DocumentProcessorController {
214235
this.logger.log(`Completed async processing for report: ${reportId}`);
215236
} catch (error) {
216237
// If processing fails, update the report status to indicate failure
217-
try {
218-
const report = await this.reportsService.findOne(reportId, userId);
219-
if (report) {
220-
report.processingStatus = ProcessingStatus.FAILED;
221-
report.updatedAt = new Date().toISOString();
222-
await this.reportsService.updateReport(report);
223-
}
224-
} catch (updateError: unknown) {
225-
this.logger.error(
226-
`Failed to update report status after processing error: ${
227-
updateError instanceof Error ? updateError.message : 'Unknown error'
228-
}`,
229-
);
230-
}
231-
232238
this.logger.error(
233239
`Error during async processing for report ${reportId}: ${error instanceof Error ? error.message : 'Unknown error'}`,
234240
);
235-
throw error;
241+
await this.updateReportStatus(reportId, userId, ProcessingStatus.FAILED);
242+
}
243+
}
244+
245+
/**
246+
* Updates a report's processing status
247+
* @param reportId - ID of the report to update
248+
* @param userId - ID of the user who owns the report
249+
* @param status - The new processing status
250+
*/
251+
private async updateReportStatus(
252+
reportId: string,
253+
userId: string,
254+
status: ProcessingStatus,
255+
debugMessage: string | undefined = undefined,
256+
): Promise<void> {
257+
try {
258+
const report = await this.reportsService.findOne(reportId, userId);
259+
if (report) {
260+
report.processingStatus = status;
261+
report.updatedAt = new Date().toISOString();
262+
report.debugMessage = debugMessage;
263+
await this.reportsService.updateReport(report);
264+
this.logger.log(`Updated status of report ${reportId} to ${status}`);
265+
}
266+
} catch (updateError: unknown) {
267+
this.logger.error(
268+
`Failed to update report status after processing error: ${
269+
updateError instanceof Error ? updateError.message : 'Unknown error'
270+
}`,
271+
);
236272
}
237273
}
238274

backend/src/reports/models/report.model.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,7 @@ export class Report {
7474

7575
@ApiProperty({ description: 'Last update timestamp' })
7676
updatedAt: string;
77+
78+
@ApiProperty({ description: 'Optional debug message for the report' })
79+
debugMessage?: string;
7780
}

frontend/src/common/models/medicalReport.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,5 @@ export interface MedicalReport {
5858
fileSize: number;
5959
createdAt: string; // ISO date string
6060
updatedAt: string; // ISO date string
61+
debugMessage?: string; // Optional debug message for the report
6162
}

0 commit comments

Comments
 (0)