Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure errors in IF #793

Merged
merged 40 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
9da09b6
feat(util): update error usage in plugin storage
narekhovhannisyan Jun 9, 2024
e769eaa
feat(util): drop build error message helper
narekhovhannisyan Jun 9, 2024
4251823
feat(util): make error classes granular
narekhovhannisyan Jun 9, 2024
78972b1
feat(util): update errors usage in args
narekhovhannisyan Jun 9, 2024
62f70d5
feat(util): update errors usage in aggregation helper
narekhovhannisyan Jun 9, 2024
3269fc9
revert(types): drop helpers
narekhovhannisyan Jun 9, 2024
1c826ea
feat(lib): update errors usage in load
narekhovhannisyan Jun 9, 2024
85a66dd
feat(lib): update errors usage in initalize
narekhovhannisyan Jun 9, 2024
c39cbab
feat(lib): update errors usage in exhaust
narekhovhannisyan Jun 9, 2024
7316703
feat(config): update strings to include all messages
narekhovhannisyan Jun 10, 2024
d9c047e
feat(builtins): update errors usage in sum
narekhovhannisyan Jun 10, 2024
d045266
feat(builtins): update errors usage in mock observations, do cosmetic…
narekhovhannisyan Jun 10, 2024
c4ed3d8
feat(builtins): update errors usage in subtract
narekhovhannisyan Jun 10, 2024
c21fb0f
feat(builtins): update errors usage in shell
narekhovhannisyan Jun 10, 2024
8c39901
feat(builtins): update errors usage in sci-embodied
narekhovhannisyan Jun 10, 2024
0d978f4
feat(builtins): update errors usage in sci
narekhovhannisyan Jun 10, 2024
296734a
feat(builtins): update errors usage in regex
narekhovhannisyan Jun 10, 2024
c60a1ab
feat(builtins): update errors usage in multiply
narekhovhannisyan Jun 10, 2024
58bdc8e
feat(builtins): update errors usage in interpolation
narekhovhannisyan Jun 10, 2024
b11aa7e
feat(builtins): update errors usage in exponent
narekhovhannisyan Jun 10, 2024
2e2b07f
feat(builtins): update errors usage in divide
narekhovhannisyan Jun 10, 2024
c16f46c
feat(builtins): update errors usage in csv lookup, optimize strategy
narekhovhannisyan Jun 10, 2024
9b3ea3e
feat(builtins): update errors usage coefficient
narekhovhannisyan Jun 10, 2024
f088869
feat(builtins): update errors usage time-sync
narekhovhannisyan Jun 10, 2024
b34df91
feat(builtins): update errors usage group-by
narekhovhannisyan Jun 10, 2024
93adf71
feat(builtins): update errors usage export yaml
narekhovhannisyan Jun 10, 2024
b6d9b20
feat(builtins): update errors usage export csv
narekhovhannisyan Jun 10, 2024
19f1eec
feat(builtins): update errors usage export csv raw
narekhovhannisyan Jun 10, 2024
cfc89db
test(util): migrate tests to use new errors
narekhovhannisyan Jun 10, 2024
fe46a8d
test(lib): migrate tests to use new errors
narekhovhannisyan Jun 10, 2024
6cca843
test(config): drop strings tests
narekhovhannisyan Jun 10, 2024
55a9060
test(builtins): migrate tests to use updated error pattern
narekhovhannisyan Jun 10, 2024
35b7657
test(builtins): migrate errors, add cases to csv lookup
narekhovhannisyan Jun 10, 2024
efa095e
test(mocks): add invalid csv case to fs
narekhovhannisyan Jun 10, 2024
b78d49a
revert(builtins): remove unnecessary console.log
narekhovhannisyan Jun 12, 2024
a75e8d7
test(util): remove unnecessary console.log
narekhovhannisyan Jun 12, 2024
85409c2
chore(src): fetch and integrate changes from main
narekhovhannisyan Jun 12, 2024
196ad04
chore(src): fetch changes from main branch
narekhovhannisyan Jun 13, 2024
d5d1ea9
revert(config): remove missing manifest from stdin message
narekhovhannisyan Jun 13, 2024
f932b7d
revert(util): remove unused error class and message
narekhovhannisyan Jun 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/__mocks__/fs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ export const readFile = async (filePath: string) => {
throw new Error('file not found');
}

if (filePath.includes('fail-csv-reader.csv')) {
return `
cpu-cores-available,≈ç≈¬˚∆∑∂´®øˆ´cpu-cores-utilized, ---- cpu-manufacturer,cpu-model-name,cpu-tdp,gpu-count,gpu-model-name,Hardware Information on AWS Documentation & Comments,instance-class,instance-storage,memory-available,platform-memory,release-date,storage-drives
16,8,AWS,AWS Graviton
16,16,AWS,AWS Graviton,150.00,N/A,N/A,AWS Graviton (ARM),a1.4xlarge,EBS-Only,32,32,November 2018,`;
}

/**
* Used for csv lookup plugin.
*/
Expand Down
18 changes: 7 additions & 11 deletions src/__tests__/unit/builtins/CommonGenerator.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {KeyValuePair} from '../../../types/common';

import {ERRORS} from '../../../util/errors';

import {CommonGenerator} from '../../../builtins/mock-observations/helpers/common-generator';
import {STRINGS} from '../../../config';

const {InputValidationError} = ERRORS;
const {GlobalConfigError} = ERRORS;
const {MISSING_GLOBAL_CONFIG} = STRINGS;

describe('lib/mock-observations/CommonGenerator: ', () => {
describe('builtins/mock-observations/CommonGenerator: ', () => {
describe('initialize: ', () => {
it('throws an error when config is not empty object.', async () => {
const commonGenerator = CommonGenerator({});
Expand All @@ -16,26 +16,22 @@ describe('lib/mock-observations/CommonGenerator: ', () => {
try {
commonGenerator.next([]);
} catch (error) {
expect(error).toEqual(
new InputValidationError(
'CommonGenerator: Config must not be null or empty.'
)
);
expect(error).toEqual(new GlobalConfigError(MISSING_GLOBAL_CONFIG));
}
});
});

describe('next(): ', () => {
it('returns a result with valid data.', async () => {
const config: KeyValuePair = {
const config: Record<string, any> = {
key1: 'value1',
key2: 'value2',
};
const commonGenerator = CommonGenerator(config);

expect.assertions(1);

expect(commonGenerator.next([])).toStrictEqual({
expect(commonGenerator.next([])).toEqual({
key1: 'value1',
key2: 'value2',
});
Expand Down
25 changes: 8 additions & 17 deletions src/__tests__/unit/builtins/RandIntGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ import {ERRORS} from '../../../util/errors';

import {RandIntGenerator} from '../../../builtins/mock-observations/helpers/rand-int-generator';

const {InputValidationError} = ERRORS;
import {STRINGS} from '../../../config';

describe('lib/mock-observations/RandIntGenerator: ', () => {
const {GlobalConfigError} = ERRORS;
const {INVALID_NAME, MISSING_MIN_MAX, MISSING_GLOBAL_CONFIG} = STRINGS;

describe('builtins/mock-observations/RandIntGenerator: ', () => {
describe('initialize', () => {
it('throws an error when the generator name is empty string.', async () => {
expect.assertions(1);
try {
RandIntGenerator('', {});
} catch (error) {
expect(error).toEqual(
new InputValidationError(
'RandIntGenerator: `name` is empty or all spaces.'
)
);
expect(error).toEqual(new GlobalConfigError(INVALID_NAME));
}
});

Expand All @@ -26,11 +25,7 @@ describe('lib/mock-observations/RandIntGenerator: ', () => {
try {
RandIntGenerator('generator-name', {});
} catch (error) {
expect(error).toEqual(
new InputValidationError(
'RandIntGenerator: Config must not be null or empty.'
)
);
expect(error).toEqual(new GlobalConfigError(MISSING_GLOBAL_CONFIG));
}
});

Expand All @@ -42,11 +37,7 @@ describe('lib/mock-observations/RandIntGenerator: ', () => {
try {
RandIntGenerator('random', config);
} catch (error) {
expect(error).toEqual(
new InputValidationError(
'RandIntGenerator: Config is missing min or max.'
)
);
expect(error).toEqual(new GlobalConfigError(MISSING_MIN_MAX));
}
});
});
Expand Down
10 changes: 7 additions & 3 deletions src/__tests__/unit/builtins/coefficient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import {Coefficient} from '../../../builtins/coefficient';

import {ERRORS} from '../../../util/errors';

const {InputValidationError, ConfigNotFoundError} = ERRORS;
import {STRINGS} from '../../../config';

const {InputValidationError, GlobalConfigError} = ERRORS;
const {MISSING_GLOBAL_CONFIG} = STRINGS;

describe('builtins/coefficient: ', () => {
describe('Coefficient: ', () => {
Expand Down Expand Up @@ -49,7 +52,6 @@ describe('builtins/coefficient: ', () => {
it('throws an error when global config is not provided.', () => {
const config = undefined;
const coefficient = Coefficient(config!);
const expectedMessage = 'Global config is not provided.';

expect.assertions(1);

Expand All @@ -62,7 +64,9 @@ describe('builtins/coefficient: ', () => {
},
]);
} catch (error) {
expect(error).toStrictEqual(new ConfigNotFoundError(expectedMessage));
expect(error).toStrictEqual(
new GlobalConfigError(MISSING_GLOBAL_CONFIG)
);
}
});

Expand Down
62 changes: 48 additions & 14 deletions src/__tests__/unit/builtins/csv-lookup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ import AxiosMockAdapter from 'axios-mock-adapter';

import {CSVLookup} from '../../../builtins';

import {STRINGS} from '../../../config';

import {ERRORS} from '../../../util/errors';

const {FileNotFoundError, InputValidationError, ConfigNotFoundError} = ERRORS;
const {
GlobalConfigError,
ReadFileError,
FetchingFileError,
QueryDataNotFoundError,
MissingCSVColumnError,
CSVParseError,
} = ERRORS;
const {MISSING_GLOBAL_CONFIG, MISSING_CSV_COLUMN, NO_QUERY_DATA} = STRINGS;

describe('builtins/CSVLookup: ', () => {
const mock = new AxiosMockAdapter(axios);
Expand Down Expand Up @@ -131,7 +141,7 @@ describe('builtins/CSVLookup: ', () => {
await csvLookup.execute(input);
} catch (error) {
if (error instanceof Error) {
expect(error).toBeInstanceOf(FileNotFoundError);
expect(error).toBeInstanceOf(ReadFileError);
}
}
});
Expand Down Expand Up @@ -160,7 +170,7 @@ describe('builtins/CSVLookup: ', () => {
await csvLookup.execute(input);
} catch (error) {
if (error instanceof Error) {
expect(error).toBeInstanceOf(FileNotFoundError);
expect(error).toBeInstanceOf(ReadFileError);
}
}
});
Expand Down Expand Up @@ -192,7 +202,7 @@ describe('builtins/CSVLookup: ', () => {
await csvLookup.execute(input);
} catch (error) {
if (error instanceof Error) {
expect(error).toBeInstanceOf(FileNotFoundError);
expect(error).toBeInstanceOf(FetchingFileError);
}
}
});
Expand Down Expand Up @@ -340,11 +350,8 @@ describe('builtins/CSVLookup: ', () => {
await csvLookup.execute(input);
} catch (error) {
if (error instanceof Error) {
expect(error).toBeInstanceOf(InputValidationError);
expect(error.message).toEqual(
`Error happened while parsing given CSV file: ./file.csv
InputValidationError: One or more of the given query parameters are not found in the target CSV file column headers.`
);
expect(error).toBeInstanceOf(QueryDataNotFoundError);
expect(error.message).toEqual(NO_QUERY_DATA);
}
}
});
Expand All @@ -368,8 +375,8 @@ InputValidationError: One or more of the given query parameters are not found in
await csvLookup.execute(input);
} catch (error) {
if (error instanceof Error) {
expect(error).toBeInstanceOf(ConfigNotFoundError);
expect(error.message).toEqual('Global config is not provided.');
expect(error).toBeInstanceOf(GlobalConfigError);
expect(error.message).toEqual(MISSING_GLOBAL_CONFIG);
}
}
});
Expand Down Expand Up @@ -400,10 +407,9 @@ InputValidationError: One or more of the given query parameters are not found in
await csvLookup.execute(input);
} catch (error) {
if (error instanceof Error) {
expect(error).toBeInstanceOf(InputValidationError);
expect(error).toBeInstanceOf(MissingCSVColumnError);
expect(error.message).toEqual(
`Error happened while parsing given CSV file: ./file.csv
InputValidationError: There is no column with name: mock.`
MISSING_CSV_COLUMN(globalConfig.output)
);
}
}
Expand Down Expand Up @@ -477,5 +483,33 @@ InputValidationError: There is no column with name: mock.`
expect(result).toStrictEqual(expectedResult);
});
});

it('rejects with CSV parse error', async () => {
process.env.csv = 'fail';
expect.assertions(1);
const globalConfig = {
filepath: './fail-csv-reader.csv',
query: {
'cpu-cores-available': 'cpu/available',
'cpu-cores-utilized': 'cpu/utilized',
'cpu-manufacturer': 'cpu/manufacturer',
},
output: [['gpu-count']],
};
const csvLookup = CSVLookup(globalConfig);

try {
await csvLookup.execute([
{
timestamp: '2024-03-01',
'cpu/available': 16,
'cpu/utilized': 16,
'cpu/manufacturer': 'AWS',
},
]);
} catch (error) {
expect(error).toBeInstanceOf(CSVParseError);
}
});
});
});
17 changes: 11 additions & 6 deletions src/__tests__/unit/builtins/divide.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {Divide} from '../../../builtins';

import {ERRORS} from '../../../util/errors';
import {STRINGS} from '../../../config';

const {InputValidationError, ConfigNotFoundError} = ERRORS;
const {InputValidationError, GlobalConfigError, MissingInputDataError} = ERRORS;
const {MISSING_GLOBAL_CONFIG, MISSING_INPUT_DATA} = STRINGS;

describe('builtins/divide: ', () => {
describe('Divide: ', () => {
Expand Down Expand Up @@ -103,7 +105,6 @@ describe('builtins/divide: ', () => {
});

it('throws an error on missing global config.', async () => {
const expectedMessage = 'Global config is not provided.';
const config = undefined;
const divide = Divide(config!);

Expand All @@ -117,7 +118,9 @@ describe('builtins/divide: ', () => {
},
]);
} catch (error) {
expect(error).toStrictEqual(new ConfigNotFoundError(expectedMessage));
expect(error).toStrictEqual(
new GlobalConfigError(MISSING_GLOBAL_CONFIG)
);
}
});

Expand Down Expand Up @@ -148,8 +151,6 @@ describe('builtins/divide: ', () => {
});

it('throws an error when `denominator` is string.', async () => {
const expectedMessage = '`10` is missing from the input.';

const globalConfig = {
numerator: 'vcpus-allocated',
denominator: '10',
Expand All @@ -168,7 +169,11 @@ describe('builtins/divide: ', () => {
},
]);
} catch (error) {
expect(error).toStrictEqual(new InputValidationError(expectedMessage));
expect(error).toStrictEqual(
new MissingInputDataError(
MISSING_INPUT_DATA(globalConfig.denominator)
)
);
}
});
});
Expand Down
30 changes: 14 additions & 16 deletions src/__tests__/unit/builtins/exponent.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {Exponent} from '../../../builtins/exponent';

import {ERRORS} from '../../../util/errors';
import {STRINGS} from '../../../config';

const {InputValidationError} = ERRORS;
const {InputValidationError, MissingInputDataError} = ERRORS;
const {NOT_NUMERIC_VALUE, MISSING_INPUT_DATA} = STRINGS;

describe('lib/exponent: ', () => {
describe('builtins/exponent: ', () => {
describe('Exponent: ', () => {
const globalConfig = {
'input-parameter': 'energy/base',
Expand Down Expand Up @@ -45,9 +47,6 @@ describe('lib/exponent: ', () => {
});

it('throws an error on missing params in input.', async () => {
const expectedMessage =
'Exponent: energy/base is missing from the input array.';

expect.assertions(1);

try {
Expand All @@ -59,27 +58,26 @@ describe('lib/exponent: ', () => {
]);
} catch (error) {
expect(error).toStrictEqual(
new InputValidationError(expectedMessage)
new MissingInputDataError(MISSING_INPUT_DATA('energy/base'))
);
}
});

it('throws an error on input param value not numeric.', async () => {
const expectedMessage = 'Exponent: i-am-not-a-number is not numeric.';

expect.assertions(1);
const input = [
{
duration: 3600,
'energy/base': 'i-am-not-a-number',
timestamp: '2021-01-01T00:00:00Z',
},
];

try {
await exponent.execute([
{
duration: 3600,
'energy/base': 'i-am-not-a-number',
timestamp: '2021-01-01T00:00:00Z',
},
]);
await exponent.execute(input);
} catch (error) {
expect(error).toStrictEqual(
new InputValidationError(expectedMessage)
new InputValidationError(NOT_NUMERIC_VALUE(input[0]['energy/base']))
);
}
});
Expand Down
Loading