Skip to content

Commit 4ecc755

Browse files
authored
Merge pull request #101 from ModusCreateOrg/ADE-66
[ADE-66] fix: Enhance error handling and add debug message support in report processing
2 parents 8396993 + 97a4f5b commit 4ecc755

File tree

5 files changed

+59
-23
lines changed

5 files changed

+59
-23
lines changed

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

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,39 @@ 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+
const errorMessage = `Failed to retrieve file from S3 for report ${reportId}: ${error instanceof Error ? error.message : 'Unknown error'}`;
191+
this.logger.error(errorMessage);
192+
await this.failReport(reportId, userId, errorMessage);
193+
return;
194+
}
186195

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

190208
// Fetch the report again to ensure we have the latest version
191209
const report = await this.reportsService.findOne(reportId, userId);
192210
if (!report) {
193-
throw new Error(`Report ${reportId} not found during async processing`);
211+
this.logger.error(`Report ${reportId} not found during async processing`);
212+
return;
194213
}
195214

196215
// Update the report with analysis results
197-
report.title = result.analysis.title || 'Untitled Report';
198-
report.category = result.analysis.category || 'general';
216+
report.title = result.analysis.title;
217+
report.category = result.analysis.category;
199218
report.processingStatus = ProcessingStatus.PROCESSED;
200219

201220
// Extract lab values
@@ -214,25 +233,38 @@ export class DocumentProcessorController {
214233
this.logger.log(`Completed async processing for report: ${reportId}`);
215234
} catch (error) {
216235
// 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-
}
236+
const errorMessage = `Error during async processing for report ${reportId}: ${error instanceof Error ? error.message : 'Unknown error'}`;
237+
this.logger.error(errorMessage);
238+
await this.failReport(reportId, userId, errorMessage);
239+
}
240+
}
231241

242+
/**
243+
* Updates a report's processing status to FAILED and logs a debug message
244+
* @param reportId - ID of the report to update
245+
* @param userId - ID of the user who owns the report
246+
* @param debugMessage - Optional debug message describing the failure
247+
*/
248+
private async failReport(
249+
reportId: string,
250+
userId: string,
251+
debugMessage: string | undefined = undefined,
252+
): Promise<void> {
253+
try {
254+
const report = await this.reportsService.findOne(reportId, userId);
255+
if (report) {
256+
report.processingStatus = ProcessingStatus.FAILED;
257+
report.updatedAt = new Date().toISOString();
258+
report.debugMessage = debugMessage;
259+
await this.reportsService.updateReport(report);
260+
this.logger.log(`Updated status of report ${reportId} to FAILED`);
261+
}
262+
} catch (updateError: unknown) {
232263
this.logger.error(
233-
`Error during async processing for report ${reportId}: ${error instanceof Error ? error.message : 'Unknown error'}`,
264+
`Failed to update report status after processing error: ${
265+
updateError instanceof Error ? updateError.message : 'Unknown error'
266+
}`,
234267
);
235-
throw error;
236268
}
237269
}
238270

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
}

frontend/src/common/utils/i18n/resources/en/reportDetail.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,4 @@
9090
"original-report": "Original Report"
9191
}
9292
}
93-
}
93+
}

frontend/src/pages/Reports/ReportDetailPage.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@
204204
}
205205

206206
&__section-empty-icon {
207-
background-color: #EEF1FF;
207+
background-color: #eef1ff;
208208
border-radius: 50%;
209209
width: 90px;
210210
height: 90px;

0 commit comments

Comments
 (0)