From ab93a4784006dec6b74975dd41fd9c26b5caee16 Mon Sep 17 00:00:00 2001 From: Ruairidh Taylor Date: Thu, 2 May 2024 17:07:00 +0100 Subject: [PATCH] chore(cb2-11044): remove try catch --- src/services/CertificateGenerationService.ts | 114 ++++++++++--------- src/services/LambdaService.ts | 6 +- src/services/S3BucketService.ts | 18 +-- 3 files changed, 62 insertions(+), 76 deletions(-) diff --git a/src/services/CertificateGenerationService.ts b/src/services/CertificateGenerationService.ts index a062d17e..d6a7c3e7 100644 --- a/src/services/CertificateGenerationService.ts +++ b/src/services/CertificateGenerationService.ts @@ -804,67 +804,67 @@ class CertificateGenerationService { return this.lambdaClient .invoke(invokeParams) - .then( async ( - response: InvocationResponse - ) => { - console.warn("validateInvocationResponse called in getOdometerHistory"); - const payload: any = await this.lambdaClient.validateInvocationResponse(response); - // TODO: convert to correct type - const testResults: any[] = JSON.parse(payload.body); - - if (!testResults || testResults.length === 0) { - throw new HTTPError( - 400, - `${ERRORS.LAMBDA_INVOCATION_BAD_DATA} ${JSON.stringify(payload)}.` - ); - } - // Sort results by testEndTimestamp - testResults.sort((first: any, second: any): number => { - if ( - moment(first.testEndTimestamp).isBefore(second.testEndTimestamp) - ) { - return 1; - } - - if ( - moment(first.testEndTimestamp).isAfter(second.testEndTimestamp) - ) { - return -1; - } - - return 0; - }); + .then(async ( + response: InvocationResponse + ) => { + console.warn("validateInvocationResponse called in getOdometerHistory"); + const payload: any = await this.lambdaClient.validateInvocationResponse(response); + // TODO: convert to correct type + const testResults: any[] = JSON.parse(payload.body); - // Remove the first result as it should be the current one. - testResults.shift(); + if (!testResults || testResults.length === 0) { + throw new HTTPError( + 400, + `${ERRORS.LAMBDA_INVOCATION_BAD_DATA} ${JSON.stringify(payload)}.` + ); + } + // Sort results by testEndTimestamp + testResults.sort((first: any, second: any): number => { + if ( + moment(first.testEndTimestamp).isBefore(second.testEndTimestamp) + ) { + return 1; + } - // Set the array to only submitted tests (exclude cancelled) - const submittedTests = testResults.filter((testResult) => { - return testResult.testStatus === "submitted"; - }); + if ( + moment(first.testEndTimestamp).isAfter(second.testEndTimestamp) + ) { + return -1; + } - const filteredTestResults = submittedTests - .filter(({ testTypes }) => - testTypes?.some( - (testType: ITestType) => - testType.testTypeClassification === - "Annual With Certificate" && - (testType.testResult === "pass" || - testType.testResult === "prs") - ) + return 0; + }); + + // Remove the first result as it should be the current one. + testResults.shift(); + + // Set the array to only submitted tests (exclude cancelled) + const submittedTests = testResults.filter((testResult) => { + return testResult.testStatus === "submitted"; + }); + + const filteredTestResults = submittedTests + .filter(({ testTypes }) => + testTypes?.some( + (testType: ITestType) => + testType.testTypeClassification === + "Annual With Certificate" && + (testType.testResult === "pass" || + testType.testResult === "prs") ) - .slice(0, 3); // Only last three entries are used for the history. + ) + .slice(0, 3); // Only last three entries are used for the history. - return { - OdometerHistoryList: filteredTestResults.map((testResult) => { - return { - value: testResult.odometerReading, - unit: testResult.odometerReadingUnits, - date: moment(testResult.testEndTimestamp).format("DD.MM.YYYY"), - }; - }), - }; - } + return { + OdometerHistoryList: filteredTestResults.map((testResult) => { + return { + value: testResult.odometerReading, + unit: testResult.odometerReadingUnits, + date: moment(testResult.testEndTimestamp).format("DD.MM.YYYY"), + }; + }), + }; + } ) .catch((error: ServiceException | Error) => { console.log(error); @@ -1048,6 +1048,8 @@ class CertificateGenerationService { throw new HTTPError(400, `${ERRORS.LAMBDA_INVOCATION_BAD_DATA} ${JSON.stringify(payload)}.`); } defects = defectsParsed; + console.log("retries ~: ", retries); + console.log("defects ~: ", defects); return defects; } catch (error) { retries++; diff --git a/src/services/LambdaService.ts b/src/services/LambdaService.ts index 42673a83..f401ab09 100644 --- a/src/services/LambdaService.ts +++ b/src/services/LambdaService.ts @@ -26,11 +26,7 @@ class LambdaService { public async invoke( params: InvocationRequest ): Promise { - try { - return await this.lambdaClient.send(new InvokeCommand(params)); - } catch (err) { - throw err; - } + return await this.lambdaClient.send(new InvokeCommand(params)); } /** diff --git a/src/services/S3BucketService.ts b/src/services/S3BucketService.ts index e0866c20..bfb77b9a 100644 --- a/src/services/S3BucketService.ts +++ b/src/services/S3BucketService.ts @@ -37,11 +37,7 @@ class S3BucketService { Metadata: metadata, }); - try { - return await this.s3Client.send(command); - } catch (err) { - throw err; - } + return await this.s3Client.send(command); } /** @@ -58,11 +54,7 @@ class S3BucketService { Key: `${process.env.BRANCH}/${fileName}`, }); - try { - return this.s3Client.send(command); - } catch (err) { - throw err; - } + return this.s3Client.send(command); } /** @@ -79,11 +71,7 @@ class S3BucketService { Key: `${process.env.BRANCH}/${fileName}`, }); - try { - return this.s3Client.send(command); - } catch (err) { - throw err; - } + return this.s3Client.send(command); } }