Skip to content

Commit

Permalink
refactor: handle exception case for construct identifier
Browse files Browse the repository at this point in the history
Signed-off-by: Nam Hoang <[email protected]>
  • Loading branch information
namhoang1604 committed Sep 20, 2024
1 parent a245679 commit 7cda210
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
50 changes: 50 additions & 0 deletions packages/services/src/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,31 @@ describe('concatService', () => {
const result = concatService(data, ...args);
expect(result).toBe('(01)05012345678900(10)2024001(21)TRF240001');
});

it('should return an empty string if the path does not exist', () => {
const data = {
test: 'test',
};
const args: any = [{ type: 'path', value: '/something' }];
const result = concatService(data, ...args);
expect(result).toBe('');
});

it('should throw an error if the path is invalid', () => {
const data = {
test: 'test',
};
const args: any = [{ type: 'path', value: 'invalid' }];

expect(() => concatService(data, ...args)).toThrow('Error concatenating values');
});

it('should throw an error if the data is not an object', () => {
const data = 'test';
const args: any = [{ type: 'path', value: '/test' }];

expect(() => concatService(data, ...args)).toThrow('Invalid data object');
});
});

describe('constructIdentifierString', () => {
Expand Down Expand Up @@ -186,4 +211,29 @@ describe('constructIdentifierString', () => {
const result = constructIdentifierString(data, identifierKeyPath);
expect(result).toBe('05012345678900');
});

it('should return an empty string if the path does not exist', () => {
const data = {
test: 'test',
};
const identifierKeyPath = '/something';
const result = constructIdentifierString(data, identifierKeyPath);
expect(result).toBe('');
});

it('should throw an error if the path is invalid', () => {
const data = {
test: 'test',
};
const identifierKeyPath = 'invalid';

expect(() => constructIdentifierString(data, identifierKeyPath)).toThrow('Error constructing identifier string');
});

it('should throw an error if the data is not an object', () => {
const data = 'test';
const identifierKeyPath = '/test';

expect(() => constructIdentifierString(data, identifierKeyPath)).toThrow('Invalid data object');
});
});
27 changes: 24 additions & 3 deletions packages/services/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,17 @@ export const concatService = (data: any, ...args: ConcatArgument[]): string => {
if (arg.type === ConcatArgumentType.TEXT) {
return acc.concat(arg.value);
} else if (arg.type === ConcatArgumentType.PATH) {
return acc.concat(JSONPointer.get(data, arg.value));
if (typeof data !== 'object') {
throw new Error('Invalid data object');
}
try {
const value = JSONPointer.get(data, arg.value);
if (value) {
return acc.concat(value);
}
} catch (error) {
throw new Error('Error concatenating values');
}
}
return acc;
}, '');
Expand Down Expand Up @@ -295,8 +305,20 @@ export const constructIdentifierString = (
data: any,
identifierKeyPath: string | { function: string; args: any },
): string => {
if (typeof data !== 'object') {
throw new Error('Invalid data object');
}

if (typeof identifierKeyPath === 'string') {
return JSONPointer.get(data, identifierKeyPath);
try {
const value = JSONPointer.get(data, identifierKeyPath);
if (value) {
return value;
}
return '';
} catch (error) {
throw new Error('Error constructing identifier string');
}
} else {
try {
const handlerFunction: any = supportedHandlerFunctions[identifierKeyPath.function];
Expand All @@ -305,7 +327,6 @@ export const constructIdentifierString = (
}
return handlerFunction(data, ...identifierKeyPath.args);
} catch (error: any) {
console.log(error);
throw new Error('Error constructing identifier string using handler function');
}
}
Expand Down

0 comments on commit 7cda210

Please sign in to comment.