diff --git a/packages/core/src/utils/errors.ts b/packages/core/src/utils/errors.ts deleted file mode 100644 index a97026c67..000000000 --- a/packages/core/src/utils/errors.ts +++ /dev/null @@ -1,21 +0,0 @@ -/** - * Collection of error messages grouped by domain. - * @constant - */ -const ERRORS = { - /** - * Error messages related to data validations. - */ - DATA: { - /** - * Error message for invalid data type - * @param format - The expected data type - * @returns The error message - */ - INVALID_DATA_TYPE: function (format: string): string { - return `Invalid data type. Data should be ${format}.`; - } - } -}; - -export { ERRORS }; diff --git a/packages/core/src/utils/index.ts b/packages/core/src/utils/index.ts index 86673174a..3465c33d7 100644 --- a/packages/core/src/utils/index.ts +++ b/packages/core/src/utils/index.ts @@ -1,5 +1,4 @@ export * from './const'; -export * from './errors'; export * from './bloom'; export * from './data'; export * from './transaction'; diff --git a/packages/core/src/utils/transaction/transaction.ts b/packages/core/src/utils/transaction/transaction.ts index e42cd62b8..aadd91375 100644 --- a/packages/core/src/utils/transaction/transaction.ts +++ b/packages/core/src/utils/transaction/transaction.ts @@ -2,7 +2,7 @@ import { address } from '../../address'; import { type TransactionClause } from '../../transaction'; import { TRANSACTIONS_GAS_CONSTANTS } from '../const'; import { dataUtils } from '../data'; -import { ERRORS } from '../errors'; +import { buildError, DATA } from '@vechain-sdk/errors'; /** * Calculates intrinsic gas that a tx costs with the given set of clauses. @@ -26,7 +26,10 @@ function intrinsicGas(clauses: TransactionClause[]): number { if (clause.to !== null) { // Invalid address if (!address.isAddress(clause.to)) { - throw new Error(ERRORS.DATA.INVALID_DATA_TYPE('an address')); + throw buildError( + DATA.INVALID_DATA_TYPE, + 'Invalid data type. Data should be an address.' + ); } sum += TRANSACTIONS_GAS_CONSTANTS.CLAUSE_GAS; @@ -47,7 +50,10 @@ function intrinsicGas(clauses: TransactionClause[]): number { function _calculateDataUsedGas(data: string): number { // Invalid data if (data !== '' && !dataUtils.isHexString(data)) { - throw new Error(ERRORS.DATA.INVALID_DATA_TYPE('a hexadecimal string')); + throw buildError( + DATA.INVALID_DATA_TYPE, + 'Invalid data type. Data should be an hexadecimal string.' + ); } let sum = 0; diff --git a/packages/core/tests/utils/errors/errors.test.ts b/packages/core/tests/utils/errors/errors.test.ts deleted file mode 100644 index 54a67e697..000000000 --- a/packages/core/tests/utils/errors/errors.test.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { describe, expect, test } from '@jest/globals'; -import { ERRORS } from '../../../src'; - -/** - * Data errors - * @group unit/utils-errors - */ -describe('Data Errors', () => { - /** - * Data format - */ - test('Should generate an error message indicating an invalid data type', () => { - expect(ERRORS.DATA.INVALID_DATA_TYPE('a hexadecimal string')).toBe( - 'Invalid data type. Data should be a hexadecimal string.' - ); - }); - - /** - * Dynamic error messages - */ - test('Should generate dynamic error messages based on the provided format', () => { - expect(ERRORS.DATA.INVALID_DATA_TYPE('a positive integer')).toBe( - 'Invalid data type. Data should be a positive integer.' - ); - }); -}); diff --git a/packages/core/tests/utils/transaction/fixture.ts b/packages/core/tests/utils/transaction/fixture.ts index 999ade1c4..7a4cfcc73 100644 --- a/packages/core/tests/utils/transaction/fixture.ts +++ b/packages/core/tests/utils/transaction/fixture.ts @@ -1,5 +1,8 @@ -import { ERRORS, TRANSACTIONS_GAS_CONSTANTS } from '../../../src'; -import { type TransactionClause } from '../../../src'; +import { + TRANSACTIONS_GAS_CONSTANTS, + type TransactionClause +} from '../../../src'; +import { InvalidDataTypeError } from '@vechain-sdk/errors'; /** * Invalid clauses data fixture @@ -10,14 +13,14 @@ const invalidData = [ to: 'INVALID_ADDRESS', value: 0, data: '', - errorToThrow: ERRORS.DATA.INVALID_DATA_TYPE('an address') + errorToThrow: InvalidDataTypeError }, // Invalid data field format for smart contract creation { to: null, value: 0, data: 'INVALID_DATA_FORMAT', - errorToThrow: ERRORS.DATA.INVALID_DATA_TYPE('a hexadecimal string') + errorToThrow: InvalidDataTypeError } ]; diff --git a/yarn.lock b/yarn.lock index 68ea04c26..b5b864771 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1286,14 +1286,14 @@ acorn-jsx@^5.3.2: integrity sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ== acorn-walk@^8.1.1: - version "8.2.0" - resolved "https://registry.yarnpkg.com/acorn-walk/-/acorn-walk-8.2.0.tgz#741210f2e2426454508853a2f44d0ab83b7f69c1" - integrity sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA== + version "8.3.0" + resolved "https://registry.yarnpkg.com/acorn-walk/-/acorn-walk-8.3.0.tgz#2097665af50fd0cf7a2dfccd2b9368964e66540f" + integrity sha512-FS7hV565M5l1R08MXqo8odwMTB02C2UqzB17RVgu9EyuYFBqJZ3/ZY97sQD5FewVu1UyDFc1yztUDrAwT0EypA== acorn@^8.4.1, acorn@^8.9.0: - version "8.10.0" - resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.10.0.tgz#8be5b3907a67221a81ab23c7889c4c5526b62ec5" - integrity sha512-F0SAmZ8iUtS//m8DmCTA0jlh6TDKkHQyK6xc6V4KDTyZKA9dnvX9/3sRTVQrWm79glUAZbnmmNcdYwUIHWVybw== + version "8.11.2" + resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.11.2.tgz#ca0d78b51895be5390a5903c5b3bdcdaf78ae40b" + integrity sha512-nc0Axzp/0FILLEVsm4fNwLCwMttvhEI263QtVPQcbpfZZ3ts0hLsZGOpE6czNlid7CJ9MlyH8reXkpsf3YUY4w== aes-js@3.0.0: version "3.0.0" @@ -1483,9 +1483,9 @@ axios@^0.27.2: form-data "^4.0.0" axios@^1.5.1: - version "1.5.1" - resolved "https://registry.yarnpkg.com/axios/-/axios-1.5.1.tgz#11fbaa11fc35f431193a9564109c88c1f27b585f" - integrity sha512-Q28iYCWzNHjAm+yEAot5QaAMxhMghWLFVf7rRdwhUI+c2jix2DUXjAHXVi+s1ibs3mjPO/cCgbA++3BjD0vP/A== + version "1.6.0" + resolved "https://registry.yarnpkg.com/axios/-/axios-1.6.0.tgz#f1e5292f26b2fd5c2e66876adc5b06cdbd7d2102" + integrity sha512-EZ1DYihju9pwVB+jg67ogm+Tmqc6JmhamRN6I4Zt8DfZu5lbcQGw3ozH9lFejSJgs/ibaef3A9PMXPLeefFGJg== dependencies: follow-redirects "^1.15.0" form-data "^4.0.0" @@ -2110,9 +2110,9 @@ duplexer2@~0.1.4: readable-stream "^2.0.2" electron-to-chromium@^1.4.535: - version "1.4.567" - resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.567.tgz#c92e8fbc2bd15df3068d92571733a218a5413add" - integrity sha512-8KR114CAYQ4/r5EIEsOmOMqQ9j0MRbJZR3aXD/KFA8RuKzyoUB4XrUCg+l8RUGqTVQgKNIgTpjaG8YHRPAbX2w== + version "1.4.568" + resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.568.tgz#6ab444e120dd7fd9c3789eac54d3132b6cfcd0f9" + integrity sha512-3TCOv8+BY6Ltpt1/CmGBMups2IdKOyfEmz4J8yIS4xLSeMm0Rf+psSaxLuswG9qMKt+XbNbmADybtXGpTFlbDg== elliptic@6.5.4, elliptic@^6.5.4: version "6.5.4"