Skip to content

Commit

Permalink
fix: integrate new error handling in RLP
Browse files Browse the repository at this point in the history
  • Loading branch information
rodolfopietro97 committed Oct 27, 2023
1 parent 7e85324 commit ee91b9c
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 176 deletions.
25 changes: 7 additions & 18 deletions packages/core/src/encoding/rlp/helpers/numerickind.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dataUtils, ERRORS } from '../../../utils';
import { dataUtils } from '../../../utils';
import { type RLPInput } from '../types';
import { createRlpError } from './profiles';

Expand All @@ -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);
Expand All @@ -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');
}
};

Expand All @@ -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');
}
};

Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/encoding/rlp/helpers/profiles.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { ERRORS } from '../../../utils';
import { buildError, type ErrorType, RLP } from '@vechain-sdk/errors';

/**
* Create a context-aware RLP error.
* @param context - Contextual information for enhanced error detail.
* @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<RLP.INVALID_RLP> => {
return buildError(RLP.INVALID_RLP, message, { context });
};

export { createRlpError };
12 changes: 0 additions & 12 deletions packages/core/src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
45 changes: 15 additions & 30 deletions packages/core/tests/rlp/helpers.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
];

Expand Down Expand Up @@ -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
}
];

Expand Down Expand Up @@ -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'
}
];

Expand All @@ -163,8 +152,7 @@ const invalidFixedHexBlobKindDataTestCases = [
{
data: '0x1234567890',
context: 'testContext',
bytes: 1,
expected: 'expected hex string to be 1 bytes'
bytes: 1
}
];

Expand All @@ -185,8 +173,7 @@ const invalidFixedHexBlobKindBufferTestCases = [
{
buffer: Buffer.from([1, 2]),
context: 'testContext',
bytes: 1,
expected: 'expected buffer to be 1 bytes'
bytes: 1
}
];

Expand All @@ -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
}
];

Expand Down
60 changes: 28 additions & 32 deletions packages/core/tests/rlp/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, test } from '@jest/globals';
import {
ERRORS,
assertValidNumericKindBuffer,
validateNumericKindData,
assertValidHexBlobKindData,
Expand All @@ -25,6 +24,7 @@ import {
validNumericBufferTestCases,
validateNumberTestCases
} from './helpers.fixture';
import { InvalidRLPError } from '@vechain-sdk/errors';

/**
* Test suite for NumericKind helpers.
Expand Down Expand Up @@ -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);
});
});
});
Expand Down Expand Up @@ -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);
});
}
);
Expand All @@ -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);
});
});
});

/**
Expand All @@ -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);
});
});
});
});

Expand All @@ -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);
});
}
);
Expand All @@ -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);
});
}
);
Expand Down Expand Up @@ -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);
});
}
);
Expand Down
Loading

0 comments on commit ee91b9c

Please sign in to comment.