Skip to content

Commit

Permalink
SK-1621: fix parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
“amith-skyflow” committed Oct 23, 2024
1 parent d49a85c commit d54d4c9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
20 changes: 8 additions & 12 deletions src/vault/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,21 @@ class VaultClient {
});

private handleJsonError(err: any, data: any, requestId: string, reject: Function) {
try {
let description = JSON.parse(JSON.stringify(data));
const statusCode = description?.error?.http_status;
const grpcCode = description?.error?.grpc_code;
const details = description?.error?.details;

description = description?.error?.message || description;
this.logAndRejectError(description, err, requestId, reject, statusCode, grpcCode, details);
} catch (err) {
this.logAndRejectError(errorMessages.INVAILD_JSON_RESPONSE, err, requestId, reject, 500, undefined, undefined);
}
let description = data;
const statusCode = description?.error?.http_status;
const grpcCode = description?.error?.grpc_code;
const details = description?.error?.details;

description = description?.error?.message || description;
this.logAndRejectError(description, err, requestId, reject, statusCode, grpcCode, details);
}

private handleTextError(err: any, data: any, requestId: string, reject: Function) {
this.logAndRejectError(data, err, requestId, reject);
}

private handleGenericError(err: any, requestId: string, reject: Function) {
const description = errorMessages.ERROR_OCCURRED;
const description = err?.message || errorMessages.ERROR_OCCURRED;
this.logAndRejectError(description, err, requestId, reject);
}

Expand Down
4 changes: 2 additions & 2 deletions src/vault/controller/vault/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class VaultController {
};

if (!records || !Array.isArray(records) || records.length === 0) {
return new InsertResponse({ errors: [] });
return new InsertResponse({ insertedFields:[], errors: [] });
}

records.forEach((record, index) => {
Expand Down Expand Up @@ -132,7 +132,7 @@ class VaultController {
this.client.initAPI(authInfo, requestType);
apiCall({ headers: { ...sdkHeaders } })
.then((response: any) => {
const data = JSON.parse(JSON.stringify(response.data));
const data = response.data;
printLog(logs.infoLogs[`${requestType}_REQUEST_RESOLVED`], MessageType.LOG, this.client.getLogLevel());
switch (requestType) {
case TYPES.INSERT:
Expand Down
4 changes: 2 additions & 2 deletions src/vault/model/response/insert/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { insertResponseType } from "../../../types";
class InsertResponse {

//fields
insertedFields?: Array<insertResponseType>;
insertedFields: Array<insertResponseType>;

errors?: object;

constructor({ insertedFields, errors }: { insertedFields?: Array<insertResponseType>, errors?: object }) {
constructor({ insertedFields, errors }: { insertedFields: Array<insertResponseType>, errors?: object }) {
this.insertedFields = insertedFields;
this.errors = errors;
}
Expand Down
29 changes: 18 additions & 11 deletions test/vault/controller/vault.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ describe('VaultController insert method', () => {
const response = await vaultController.insert(mockRequest, mockOptions);

expect(mockVaultClient.vaultAPI.recordServiceBatchOperation).toHaveBeenCalled();
expect(response.insertedFields).toBe(undefined);
expect(response.insertedFields).toStrictEqual([]);
});

test('should reject insert records with batch insert', async () => {
Expand All @@ -316,7 +316,7 @@ describe('VaultController insert method', () => {
const response = await vaultController.insert(mockRequest, mockOptions);

expect(mockVaultClient.vaultAPI.recordServiceBatchOperation).toHaveBeenCalled();
expect(response.insertedFields).toBe(undefined);
expect(response.insertedFields).toStrictEqual([]);
});

test('should handle validation errors', async () => {
Expand Down Expand Up @@ -522,11 +522,14 @@ describe('VaultController detokenize method', () => {
getDownloadURL: jest.fn().mockReturnValue(false)
};

validateDetokenizeRequest.mockImplementation(() => {
// throw new Error('Validation error');
});
const errorResponse = new Error("Invalid");
mockVaultClient.tokensAPI.recordServiceDetokenize.mockRejectedValueOnce(errorResponse);

await expect(vaultController.detokenize(mockRequest, mockOptions)).rejects.toThrow('Validation error');
expect(mockVaultClient.tokensAPI.recordServiceDetokenize).not.toHaveBeenCalled();
await expect(vaultController.detokenize(mockRequest, mockOptions)).rejects.toThrow('Invalid');
expect(mockVaultClient.tokensAPI.recordServiceDetokenize).toHaveBeenCalled();
});

test('should log and resolve with empty arrays when no records are returned', async () => {
Expand Down Expand Up @@ -733,12 +736,15 @@ describe('VaultController tokenize method', () => {
const mockRequest = {
values: [{ value: 'sensitiveData', columnGroup: 'group1' }],
};
const errorResponse = new Error('Validation error');
validateTokenizeRequest.mockImplementation(() => {
// throw new Error('Validation error');
});
const errorResponse = new Error('Invalid');

mockVaultClient.tokensAPI.recordServiceTokenize.mockRejectedValueOnce(errorResponse.message);

await expect(vaultController.tokenize(mockRequest)).rejects.toEqual(errorResponse);
expect(mockVaultClient.tokensAPI.recordServiceTokenize).not.toHaveBeenCalled();
expect(mockVaultClient.tokensAPI.recordServiceTokenize).toHaveBeenCalled();
});

test('should reject when API returns no tokens', async () => {
Expand Down Expand Up @@ -1307,19 +1313,20 @@ describe('VaultController get method', () => {

test('should handle API errors during get', async () => {
const mockRequest = createGetRequest(['id1']);
const errorResponse = new Error('Validation error');

const errorResponse = new Error('Invalid');
validateGetRequest.mockImplementation(() => {
});
mockVaultClient.vaultAPI.recordServiceBulkGetRecord.mockRejectedValueOnce(errorResponse);

await expect(vaultController.get(mockRequest)).rejects.toEqual(errorResponse);

// Validate that the API call was made
expect(mockVaultClient.vaultAPI.recordServiceBulkGetRecord).not.toHaveBeenCalled();
expect(mockVaultClient.vaultAPI.recordServiceBulkGetRecord).toHaveBeenCalled();
});

test('should log and reject errors during get', async () => {
const mockRequest = createGetRequest(['id1']);
const errorResponse = new Error('Validation error');
const errorResponse = new Error('Invalid');

mockVaultClient.vaultAPI.recordServiceBulkGetRecord.mockRejectedValueOnce(errorResponse);

Expand Down

0 comments on commit d54d4c9

Please sign in to comment.