diff --git a/packages/core/src/encoding/rlp/helpers/numerickind.ts b/packages/core/src/encoding/rlp/helpers/numerickind.ts index 1edd33348..e901b8525 100644 --- a/packages/core/src/encoding/rlp/helpers/numerickind.ts +++ b/packages/core/src/encoding/rlp/helpers/numerickind.ts @@ -1,4 +1,4 @@ -import { dataUtils, ERRORS } from '../../../utils'; +import { dataUtils } from '../../../utils'; import { type RLPInput } from '../types'; import { createRlpError } from './profiles'; @@ -17,9 +17,7 @@ const validateNumericKindData = (data: RLPInput, context: string): bigint => { } else if (typeof data === 'string') { _validateNumericKindString(data, context); } else { - throw new Error( - ERRORS.RLP.INVALID_RLP(context, 'expected string or number') - ); + throw createRlpError(context, 'expected string or number'); } return BigInt(data); @@ -39,12 +37,7 @@ const validateNumericKindData = (data: RLPInput, context: string): bigint => { */ const _validateNumericKindNumber = (num: number, context: string): void => { if (!Number.isSafeInteger(num) || num < 0) { - throw new Error( - ERRORS.RLP.INVALID_RLP( - context, - 'expected non-negative safe integer' - ) - ); + throw createRlpError(context, 'expected non-negative safe integer'); } }; @@ -65,19 +58,15 @@ const _validateNumericKindString = (str: string, context: string): void => { const isDecimal = dataUtils.isDecimalString(str); if (!isHex && !isDecimal) { - throw new Error( - ERRORS.RLP.INVALID_RLP( - context, - 'expected non-negative integer in hex or dec string' - ) + throw createRlpError( + context, + 'expected non-negative integer in hex or dec string' ); } // Ensure hex numbers are of a valid length. if (isHex && str.length <= 2) { - throw new Error( - ERRORS.RLP.INVALID_RLP(context, 'expected valid hex string number') - ); + throw createRlpError(context, 'expected valid hex string number'); } }; diff --git a/packages/core/src/encoding/rlp/helpers/profiles.ts b/packages/core/src/encoding/rlp/helpers/profiles.ts index 1d0649614..1b723138e 100644 --- a/packages/core/src/encoding/rlp/helpers/profiles.ts +++ b/packages/core/src/encoding/rlp/helpers/profiles.ts @@ -1,4 +1,4 @@ -import { ERRORS } from '../../../utils'; +import { buildError, type ErrorType, RLP } from '@vechain-sdk/errors'; /** * Create a context-aware RLP error. @@ -6,8 +6,11 @@ import { ERRORS } from '../../../utils'; * @param message - Descriptive error message. * @returns An Error object tailored for RLP issues. */ -const createRlpError = (context: string, message: string): Error => { - return new Error(ERRORS.RLP.INVALID_RLP(context, message)); +const createRlpError = ( + context: string, + message: string +): ErrorType => { + return buildError(RLP.INVALID_RLP, message, { context }); }; export { createRlpError }; diff --git a/packages/core/src/utils/errors.ts b/packages/core/src/utils/errors.ts index 85fd94e24..481e2c399 100644 --- a/packages/core/src/utils/errors.ts +++ b/packages/core/src/utils/errors.ts @@ -62,18 +62,6 @@ const ERRORS = { } }, - RLP: { - /** - * Error message for invalid RLP - * @param context - The context of the error - * @param message - The error message - * @returns The error message - */ - INVALID_RLP: function (context: string, message: string): string { - return `${context}: ${message}`; - } - }, - /** * Error messages related to transactions. */ diff --git a/packages/core/tests/rlp/helpers.fixture.ts b/packages/core/tests/rlp/helpers.fixture.ts index 4756a2d31..b6862c127 100644 --- a/packages/core/tests/rlp/helpers.fixture.ts +++ b/packages/core/tests/rlp/helpers.fixture.ts @@ -14,33 +14,27 @@ const validateNumberTestCases = [ const invalidNumberTestCases = [ { number: {}, - context: 'testContext', - expected: 'expected string or number' + context: 'testContext' }, { number: -1, - context: 'testContext', - expected: 'expected non-negative safe integer' + context: 'testContext' }, { number: Number.MAX_SAFE_INTEGER + 1, - context: 'testContext', - expected: 'expected non-negative safe integer' + context: 'testContext' }, { number: 'zgy', - context: 'testContext', - expected: 'expected non-negative integer in hex or dec string' + context: 'testContext' }, { number: '0x', - context: 'testContext', - expected: 'expected valid hex string number' + context: 'testContext' }, { number: '-123', - context: 'testContext', - expected: 'expected non-negative integer in hex or dec string' + context: 'testContext' } ]; @@ -71,20 +65,17 @@ const invalidNumericBufferTestCases = [ { buffer: Buffer.from([1, 2, 3, 4]), context: 'testContext', - maxBytes: 3, - expected: 'expected less than 3 bytes' + maxBytes: 3 }, { buffer: Buffer.from([0, 2, 3]), context: 'testContext', - maxBytes: undefined, - expected: 'expected canonical integer (no leading zero bytes)' + maxBytes: undefined }, { buffer: Buffer.from([0]), context: 'testContext', - maxBytes: 1, - expected: 'expected canonical integer (no leading zero bytes)' + maxBytes: 1 } ]; @@ -136,13 +127,11 @@ const validHexBlobKindBufferTestCases = [ const invalidHexBlobKindBufferTestCases = [ { buffer: '0x123', - context: 'testContext', - expected: 'expected buffer' + context: 'testContext' }, { buffer: {}, - context: 'testContext', - expected: 'expected buffer' + context: 'testContext' } ]; @@ -163,8 +152,7 @@ const invalidFixedHexBlobKindDataTestCases = [ { data: '0x1234567890', context: 'testContext', - bytes: 1, - expected: 'expected hex string to be 1 bytes' + bytes: 1 } ]; @@ -185,8 +173,7 @@ const invalidFixedHexBlobKindBufferTestCases = [ { buffer: Buffer.from([1, 2]), context: 'testContext', - bytes: 1, - expected: 'expected buffer to be 1 bytes' + bytes: 1 } ]; @@ -212,14 +199,12 @@ const invalidCompactFixedHexBlobKindBufferTestCases = [ { buffer: Buffer.from([1, 2, 3]), context: 'testContext', - bytes: 2, - expected: 'expected buffer to be at most 2 bytes' + bytes: 2 }, { buffer: Buffer.from([0, 2, 3]), context: 'testContext', - bytes: 3, - expected: 'expected no leading zero bytes' + bytes: 3 } ]; diff --git a/packages/core/tests/rlp/helpers.test.ts b/packages/core/tests/rlp/helpers.test.ts index 68987065c..ecac192b2 100644 --- a/packages/core/tests/rlp/helpers.test.ts +++ b/packages/core/tests/rlp/helpers.test.ts @@ -1,6 +1,5 @@ import { describe, expect, test } from '@jest/globals'; import { - ERRORS, assertValidNumericKindBuffer, validateNumericKindData, assertValidHexBlobKindData, @@ -25,6 +24,7 @@ import { validNumericBufferTestCases, validateNumberTestCases } from './helpers.fixture'; +import { InvalidRLPError } from '@vechain-sdk/errors'; /** * Test suite for NumericKind helpers. @@ -62,14 +62,14 @@ describe('NumericKind helpers', () => { * This test iterates over cases where the input number is considered invalid, * asserting that the function throws an error with a corresponding message. */ - invalidNumberTestCases.forEach(({ number, context, expected }) => { + invalidNumberTestCases.forEach(({ number, context }) => { test(`should throw error when data is invalid ${JSON.stringify( number )}`, () => { expect(() => { // @ts-expect-error - invalid input validateNumericKindData(number, context); - }).toThrowError(ERRORS.RLP.INVALID_RLP(context, expected)); + }).toThrowError(InvalidRLPError); }); }); }); @@ -105,13 +105,13 @@ describe('NumericKind helpers', () => { * aligns with expectations. */ invalidNumericBufferTestCases.forEach( - ({ buffer, context, maxBytes, expected }) => { + ({ buffer, context, maxBytes }) => { test(`should throw error when buffer is invalid ${buffer.toString( 'hex' )}`, () => { expect(() => { assertValidNumericKindBuffer(buffer, context, maxBytes); - }).toThrowError(ERRORS.RLP.INVALID_RLP(context, expected)); + }).toThrowError(InvalidRLPError); }); } ); @@ -137,15 +137,13 @@ describe('HexBlobKind helpers', () => { }); }); - invalidHexBlobKindDataTestCases.forEach( - ({ data, context, expected }) => { - test(`should throw error when data is invalid ${data}`, () => { - expect(() => { - assertValidHexBlobKindData(data, context); - }).toThrowError(ERRORS.RLP.INVALID_RLP(context, expected)); - }); - } - ); + invalidHexBlobKindDataTestCases.forEach(({ data, context }) => { + test(`should throw error when data is invalid ${data}`, () => { + expect(() => { + assertValidHexBlobKindData(data, context); + }).toThrowError(InvalidRLPError); + }); + }); }); /** @@ -162,18 +160,16 @@ describe('HexBlobKind helpers', () => { }); }); - invalidHexBlobKindBufferTestCases.forEach( - ({ buffer, context, expected }) => { - test(`should throw error when buffer is invalid ${JSON.stringify( - buffer - )}`, () => { - expect(() => { - // @ts-expect-error - invalid input - assertValidHexBlobKindBuffer(buffer, context); - }).toThrowError(ERRORS.RLP.INVALID_RLP(context, expected)); - }); - } - ); + invalidHexBlobKindBufferTestCases.forEach(({ buffer, context }) => { + test(`should throw error when buffer is invalid ${JSON.stringify( + buffer + )}`, () => { + expect(() => { + // @ts-expect-error - invalid input + assertValidHexBlobKindBuffer(buffer, context); + }).toThrowError(InvalidRLPError); + }); + }); }); }); @@ -199,11 +195,11 @@ describe('FixedHexBlobKind helpers', () => { ); invalidFixedHexBlobKindDataTestCases.forEach( - ({ data, context, bytes, expected }) => { + ({ data, context, bytes }) => { test(`should throw error when data is invalid ${data}`, () => { expect(() => { assertFixedHexBlobKindData(data, context, bytes); - }).toThrowError(ERRORS.RLP.INVALID_RLP(context, expected)); + }).toThrowError(InvalidRLPError); }); } ); @@ -226,13 +222,13 @@ describe('FixedHexBlobKind helpers', () => { ); invalidFixedHexBlobKindBufferTestCases.forEach( - ({ buffer, context, bytes, expected }) => { + ({ buffer, context, bytes }) => { test(`should throw error when buffer is invalid ${JSON.stringify( buffer )}`, () => { expect(() => { assertFixedHexBlobKindBuffer(buffer, context, bytes); - }).toThrowError(ERRORS.RLP.INVALID_RLP(context, expected)); + }).toThrowError(InvalidRLPError); }); } ); @@ -260,13 +256,13 @@ describe('CompactFixedHexBlobKind helpers', () => { ); invalidCompactFixedHexBlobKindBufferTestCases.forEach( - ({ buffer, context, bytes, expected }) => { + ({ buffer, context, bytes }) => { test(`should throw error when buffer is invalid ${JSON.stringify( buffer )}`, () => { expect(() => { assertCompactFixedHexBlobBuffer(buffer, context, bytes); - }).toThrowError(ERRORS.RLP.INVALID_RLP(context, expected)); + }).toThrowError(InvalidRLPError); }); } ); diff --git a/packages/core/tests/rlp/rlp.fixture.ts b/packages/core/tests/rlp/rlp.fixture.ts index d98d65127..fbab86326 100644 --- a/packages/core/tests/rlp/rlp.fixture.ts +++ b/packages/core/tests/rlp/rlp.fixture.ts @@ -1,4 +1,4 @@ -import { ERRORS, RLP, type RLPValidObject, type RLPProfile } from '../../src'; +import { RLP, type RLPValidObject, type RLPProfile } from '../../src'; /* Simple RLP encode */ const encodeTestCases = [ @@ -83,47 +83,37 @@ const invalidNumericKindEncodeTestCases = [ { kind: new RLP.NumericKind(8), data: '0x123z', - description: 'invalid hex string', - throws: ERRORS.RLP.INVALID_RLP( - '', - 'expected non-negative integer in hex or dec string' - ) + description: 'invalid hex string' }, { kind: new RLP.NumericKind(8), data: {}, - description: 'invalid object', - throws: ERRORS.RLP.INVALID_RLP('', 'expected string or number') + description: 'invalid object' }, { kind: new RLP.NumericKind(8), data: '0x', - description: 'invalid hex string', - throws: ERRORS.RLP.INVALID_RLP('', 'expected valid hex string number') + description: 'invalid hex string' }, { kind: new RLP.NumericKind(8), data: -1, - description: 'negative number', - throws: ERRORS.RLP.INVALID_RLP('', 'expected non-negative safe integer') + description: 'negative number' }, { kind: new RLP.NumericKind(8), data: '0x12345678123456780', - description: 'number overflow', - throws: ERRORS.RLP.INVALID_RLP('', 'expected number in 8 bytes') + description: 'number overflow' }, { kind: new RLP.NumericKind(8), data: 2 ** 64, - description: 'number overflow', - throws: ERRORS.RLP.INVALID_RLP('', 'expected non-negative safe integer') + description: 'number overflow' }, { kind: new RLP.NumericKind(1), data: 256, - description: 'number overflow', - throws: ERRORS.RLP.INVALID_RLP('', 'expected number in 1 bytes') + description: 'number overflow' } ]; @@ -159,8 +149,7 @@ const invalidBufferKindDecodeTestCases = [ { kind: new RLP.BufferKind(), data: 42, - description: 'invalid data', - throws: ERRORS.RLP.INVALID_RLP('', 'expected buffer') + description: 'invalid data' } ]; @@ -168,17 +157,12 @@ const invalidNumericKindDecodeTestCases = [ { kind: new RLP.NumericKind(8), data: Buffer.alloc(9, 1), - description: 'buffer exceeds max bytes', - throws: ERRORS.RLP.INVALID_RLP('', 'expected less than 8 bytes') + description: 'buffer exceeds max bytes' }, { kind: new RLP.NumericKind(8), data: Buffer.from([0, 1]), - description: 'buffer with leading zero', - throws: ERRORS.RLP.INVALID_RLP( - '', - 'expected canonical integer (no leading zero bytes)' - ) + description: 'buffer with leading zero' } ]; @@ -196,20 +180,17 @@ const invalidHexBlobKindEncodeTestCases = [ { kind: new RLP.HexBlobKind(), data: '0x123z', - description: 'invalid hex string', - throws: ERRORS.RLP.INVALID_RLP('', 'expected hex string') + description: 'invalid hex string' }, { kind: new RLP.HexBlobKind(), data: {}, - description: 'invalid object', - throws: ERRORS.RLP.INVALID_RLP('', 'expected string') + description: 'invalid object' }, { kind: new RLP.HexBlobKind(), data: '0x123', - description: 'invalid hex string', - throws: ERRORS.RLP.INVALID_RLP('', 'expected even length string') + description: 'invalid hex string' } ]; @@ -227,26 +208,22 @@ const invalidFixedHexBlobEncodeTestCases = [ { kind: new RLP.FixedHexBlobKind(1), data: '0x123z', - description: 'invalid hex string', - throws: ERRORS.RLP.INVALID_RLP('', 'expected hex string') + description: 'invalid hex string' }, { kind: new RLP.FixedHexBlobKind(1), data: {}, - description: 'invalid data type', - throws: ERRORS.RLP.INVALID_RLP('', 'expected string') + description: 'invalid data type' }, { kind: new RLP.FixedHexBlobKind(1), data: '0x123', - description: 'invalid hex string', - throws: ERRORS.RLP.INVALID_RLP('', 'expected even length string') + description: 'invalid hex string' }, { kind: new RLP.FixedHexBlobKind(1), data: '0x012345', - description: 'overflow hex string', - throws: ERRORS.RLP.INVALID_RLP('', 'expected hex string to be 1 bytes') + description: 'overflow hex string' } ]; @@ -276,8 +253,7 @@ const invalidHexBlobKindDecodeTestCases = [ { kind: new RLP.HexBlobKind(), data: 42, - description: 'invalid data', - throws: ERRORS.RLP.INVALID_RLP('', 'expected buffer') + description: 'invalid data' } ]; @@ -301,20 +277,17 @@ const invalidFixedBlobKindDecodeTestCases = [ { kind: new RLP.FixedHexBlobKind(3), data: Buffer.from([1, 2]), - description: 'buffer with data', - throws: ERRORS.RLP.INVALID_RLP('', 'expected buffer to be 3 bytes') + description: 'buffer with data' }, { kind: new RLP.FixedHexBlobKind(1), data: Buffer.from([1, 2]), - description: 'buffer with data', - throws: ERRORS.RLP.INVALID_RLP('', 'expected buffer to be 1 bytes') + description: 'buffer with data' }, { kind: new RLP.FixedHexBlobKind(1), data: '0x123', - description: 'invalid data buffer', - throws: ERRORS.RLP.INVALID_RLP('', 'expected buffer') + description: 'invalid data buffer' } ]; @@ -775,26 +748,22 @@ const invalidEncodeObjectTestCases = [ { profile: bufferProfile, data: invalidBufferData, - description: 'encode buffer profile with invalid data', - throws: new Error('bar: expected buffer') + description: 'encode buffer profile with invalid data' }, { profile: numericProfileWithMaxBytes, data: numericDataInvalidArray, - description: 'encode numeric profile with invalid array', - throws: new Error('baz: expected array') + description: 'encode numeric profile with invalid array' }, { profile: numericProfileWithMaxBytes, data: numericDataWithByteOverflow, - description: 'encode numeric profile with byte overflow', - throws: new Error('foo: expected number in 1 bytes') + description: 'encode numeric profile with byte overflow' }, { profile: hexBlobProfile, data: hexBlobDataWithInvalidHex, - description: 'encode hex blob profile with invalid hex', - throws: new Error('bar: expected hex string') + description: 'encode hex blob profile with invalid hex' } ]; @@ -812,20 +781,17 @@ const invalidDecodeObjectTestCases = [ { profile: numericProfile, data: Buffer.from('c60102c3c20304', 'hex'), - description: 'decode buffer profile with invalid data', - throws: new Error('baz.#0: expected 3 items, but got 2') + description: 'decode buffer profile with invalid data' }, { profile: numericProfile, data: Buffer.from('c3010202', 'hex'), - description: 'decode buffer profile with invalid data', - throws: new Error('baz: expected array') + description: 'decode buffer profile with invalid data' }, { profile: numericProfile, data: Buffer.from('d1c7c60304c3c2070802c7c60304c3c20708', 'hex'), - description: 'decode buffer profile with invalid data', - throws: new Error('foo: expected buffer') + description: 'decode buffer profile with invalid data' } ]; diff --git a/packages/core/tests/rlp/rlp.test.ts b/packages/core/tests/rlp/rlp.test.ts index 1f7781839..4f3d28716 100644 --- a/packages/core/tests/rlp/rlp.test.ts +++ b/packages/core/tests/rlp/rlp.test.ts @@ -33,6 +33,7 @@ import { compactFixedHexBlobKindEncodeTestCases, compactFixedHexBlobKindDecodeTestCases } from './rlp.fixture'; +import { InvalidRLPError } from '@vechain-sdk/errors'; /** * Test suite for RLP encoding/decoding functionality @@ -64,12 +65,12 @@ describe('RLP', () => { // Testing BufferKind decoding functionality describe('BufferKind decode', () => { invalidBufferKindDecodeTestCases.forEach( - ({ kind, data, description, throws }) => { + ({ kind, data, description }) => { test(description, () => { expect(() => { // @ts-expect-error - invalid input kind.buffer(data, '').decode(); - }).toThrowError(throws); + }).toThrowError(InvalidRLPError); }); } ); @@ -92,12 +93,12 @@ describe('RLP', () => { ); invalidNumericKindEncodeTestCases.forEach( - ({ kind, data, description, throws }) => { + ({ kind, data, description }) => { test(description, () => { expect(() => { // @ts-expect-error - invalid input kind.data(data, '').encode(); - }).toThrowError(throws); + }).toThrowError(InvalidRLPError); }); } ); @@ -115,11 +116,11 @@ describe('RLP', () => { ); invalidNumericKindDecodeTestCases.forEach( - ({ kind, data, description, throws }) => { + ({ kind, data, description }) => { test(description, () => { expect(() => { kind.buffer(data, '').decode(); - }).toThrowError(throws); + }).toThrowError(InvalidRLPError); }); } ); @@ -143,12 +144,12 @@ describe('RLP', () => { ); invalidHexBlobKindEncodeTestCases.forEach( - ({ kind, data, description, throws }) => { + ({ kind, data, description }) => { test(description, () => { expect(() => { // @ts-expect-error - invalid input kind.data(data, '').encode(); - }).toThrowError(throws); + }).toThrowError(InvalidRLPError); }); } ); @@ -166,12 +167,12 @@ describe('RLP', () => { ); invalidHexBlobKindDecodeTestCases.forEach( - ({ kind, data, description, throws }) => { + ({ kind, data, description }) => { test(description, () => { expect(() => { // @ts-expect-error - invalid input kind.buffer(data, '').decode(); - }).toThrowError(throws); + }).toThrowError(InvalidRLPError); }); } ); @@ -195,12 +196,12 @@ describe('RLP', () => { ); invalidFixedHexBlobEncodeTestCases.forEach( - ({ kind, data, description, throws }) => { + ({ kind, data, description }) => { test(description, () => { expect(() => { // @ts-expect-error - invalid input kind.data(data, '').encode(); - }).toThrowError(throws); + }).toThrowError(InvalidRLPError); }); } ); @@ -219,12 +220,12 @@ describe('RLP', () => { ); invalidFixedBlobKindDecodeTestCases.forEach( - ({ kind, data, description, throws }) => { + ({ kind, data, description }) => { test(description, () => { expect(() => { // @ts-expect-error - invalid input kind.buffer(data, '').decode(); - }).toThrowError(throws); + }).toThrowError(InvalidRLPError); }); } ); @@ -373,13 +374,13 @@ describe('RLP', () => { // Testing encodeObject throws if data doesn't match profile describe("encodeObject throws if profile doesn't match data", () => { invalidEncodeObjectTestCases.forEach( - ({ profile, data, description, throws }) => { + ({ profile, data, description }) => { test(description, () => { const rlp = new RLP.Profiler(profile); expect(() => { rlp.encodeObject(data); - }).toThrowError(throws); + }).toThrowError(InvalidRLPError); }); } ); @@ -468,13 +469,13 @@ describe('RLP', () => { // Testing decodeObject throws if data doesn't match profile describe("decodeObject throws if data doesn't match profile", () => { invalidDecodeObjectTestCases.forEach( - ({ profile, data, description, throws }) => { + ({ profile, data, description }) => { test(description, () => { const rlp = new RLP.Profiler(profile); expect(() => { rlp.decodeObject(data); - }).toThrowError(throws); + }).toThrowError(InvalidRLPError); }); } );