Skip to content

Commit

Permalink
chore(cb2-11271): upgrade cvs-svc-test-results from aws sdk v2 to v3 (#…
Browse files Browse the repository at this point in the history
…406)

* chore(cb2-11271): add log

* feat(cb2-11271): change to error

* chore(cb2-11271): fix error handling

* chore(cb2-11271): formatting

* chore(cb2-11271): change message in check

* chore(cb2-11271): changes to error handling

* chore(cb2-11271): more changes

* chore(cb2-11271): linting

* chore(cb2-11271): remove some console logs

* chore(cb2-11271): formatting dynamo errors

* chore(cb2-11271): formatting
  • Loading branch information
owen-corrigan authored May 20, 2024
1 parent 1e33f10 commit 77ce73a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/handlers/VehicleTestController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
28 changes: 20 additions & 8 deletions src/handlers/expiry/providers/TestDataProvider.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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);
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/expiry/TestDataProvider.unitTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -354,7 +354,7 @@ describe('TestDataProvider', () => {
{} as models.ITestResultPayload,
);
} catch (e) {
expect(e).toBe(errorMsg);
expect(e.body).toEqual(error.message);
}
});
});
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/insertTestResult.unitTest.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -244,7 +244,7 @@ describe('insertTestResult', () => {
getBySystemNumber: (systemNumber: any) => Promise.resolve([]),
createSingle: () =>
Promise.reject({
statusCode: 400,
$metadata: { httpStatusCode: 400 },
message: MESSAGES.CONDITIONAL_REQUEST_FAILED,
}),
}));
Expand Down

0 comments on commit 77ce73a

Please sign in to comment.