diff --git a/src/handlers/VehicleTestController.ts b/src/handlers/VehicleTestController.ts index 879d04606..b4f566617 100644 --- a/src/handlers/VehicleTestController.ts +++ b/src/handlers/VehicleTestController.ts @@ -118,7 +118,7 @@ export class VehicleTestController implements IVehicleTestController { } catch (error) { if ( error.statusCode === 400 && - error.message === enums.MESSAGES.CONDITIONAL_REQUEST_FAILED + error.body === enums.MESSAGES.CONDITIONAL_REQUEST_FAILED ) { console.info( 'TestResultService.insertTestResult: Test Result id already exists', diff --git a/src/handlers/expiry/providers/TestDataProvider.ts b/src/handlers/expiry/providers/TestDataProvider.ts index 662778178..c1fc0f2cb 100644 --- a/src/handlers/expiry/providers/TestDataProvider.ts +++ b/src/handlers/expiry/providers/TestDataProvider.ts @@ -1,4 +1,4 @@ -import { ConditionalCheckFailedException } from '@aws-sdk/client-dynamodb'; +import { ServiceException } from '@smithy/smithy-client'; import * as enums from '../../../assets/Enums'; import * as models from '../../../models'; import { Service } from '../../../models/injector/ServiceDecorator'; @@ -29,6 +29,12 @@ export class TestDataProvider implements ITestDataProvider { 'TestDataProvider.getTestResultBySystemNumber: error-> ', error, ); + if (error instanceof ServiceException) { + throw new models.HTTPError( + error.$metadata.httpStatusCode ?? 500, + error.message, + ); + } throw error; } } @@ -47,6 +53,12 @@ export class TestDataProvider implements ITestDataProvider { 'TestDataProvider.getTestResultBySystemNumber: error-> ', error, ); + if (error instanceof ServiceException) { + throw new models.HTTPError( + error.$metadata.httpStatusCode ?? 500, + error.message, + ); + } throw error; } } @@ -73,6 +85,12 @@ export class TestDataProvider implements ITestDataProvider { ); } catch (error) { console.log('TestDataProvider.getTestHistory: error -> ', error); + if (error instanceof ServiceException) { + throw new models.HTTPError( + error.$metadata.httpStatusCode ?? 500, + error.message, + ); + } throw error; } } @@ -242,13 +260,7 @@ export class TestDataProvider implements ITestDataProvider { return result.Attributes as models.ITestResult[]; } catch (error) { console.error('TestDataProvider.insertTestResult -> ', error); - if ( - error instanceof ConditionalCheckFailedException && - error.$response?.statusCode - ) { - throw new models.HTTPError(error.$response?.statusCode, error.message); - } - throw error; + throw new models.HTTPError(error.$metadata.httpStatusCode, error.message); } } diff --git a/tests/unit/expiry/TestDataProvider.unitTest.ts b/tests/unit/expiry/TestDataProvider.unitTest.ts index b68ad70c0..391429527 100644 --- a/tests/unit/expiry/TestDataProvider.unitTest.ts +++ b/tests/unit/expiry/TestDataProvider.unitTest.ts @@ -343,9 +343,9 @@ describe('TestDataProvider', () => { it('should throw the error once it fails', async () => { testDataProvider = new TestDataProvider(); - const errorMsg = 'boom'; + const error = { $metadata: { httpStatusCode: 400 }, message: 'boom' }; MockTestResultsDAO = jest.fn().mockImplementation(() => ({ - createSingle: (something: any) => Promise.reject(errorMsg), + createSingle: (something: any) => Promise.reject(error), })); testDataProvider.testResultsDAO = new MockTestResultsDAO(); @@ -354,7 +354,7 @@ describe('TestDataProvider', () => { {} as models.ITestResultPayload, ); } catch (e) { - expect(e).toBe(errorMsg); + expect(e.body).toEqual(error.message); } }); }); diff --git a/tests/unit/insertTestResult.unitTest.ts b/tests/unit/insertTestResult.unitTest.ts index 21cd9f112..84a7c5835 100644 --- a/tests/unit/insertTestResult.unitTest.ts +++ b/tests/unit/insertTestResult.unitTest.ts @@ -1,18 +1,18 @@ import fs from 'fs'; -import path from 'path'; import { cloneDeep } from 'lodash'; -import { TestResultsService } from '../../src/services/TestResultsService'; -import { HTTPError } from '../../src/models/HTTPError'; +import path from 'path'; import { - MESSAGES, ERRORS, - VEHICLE_TYPES, - TEST_STATUS, - TEST_RESULT, + MESSAGES, TESTING_ERRORS, + TEST_RESULT, + TEST_STATUS, + VEHICLE_TYPES, } from '../../src/assets/Enums'; +import { HTTPResponse } from '../../src/models'; +import { HTTPError } from '../../src/models/HTTPError'; import { ITestResultPayload } from '../../src/models/ITestResultPayload'; -import { HTTPResponse } from '../../src/models/HTTPResponse'; +import { TestResultsService } from '../../src/services/TestResultsService'; import { ValidationUtil } from '../../src/utils/validationUtil'; describe('insertTestResult', () => { @@ -244,7 +244,7 @@ describe('insertTestResult', () => { getBySystemNumber: (systemNumber: any) => Promise.resolve([]), createSingle: () => Promise.reject({ - statusCode: 400, + $metadata: { httpStatusCode: 400 }, message: MESSAGES.CONDITIONAL_REQUEST_FAILED, }), }));