@@ -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
0 commit comments