Skip to content

Commit

Permalink
refactor: made error messages more consistent (#330)
Browse files Browse the repository at this point in the history
Co-authored-by: fabiorigam <[email protected]>
  • Loading branch information
bacco1977 and fabiorigam authored Dec 1, 2023
1 parent 40ad46a commit 6492cfb
Show file tree
Hide file tree
Showing 32 changed files with 111 additions and 83 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/abi/coder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function encode<ValueType>(type: string | ParamType, value: ValueType): string {
} catch {
throw buildError(
ABI.INVALID_DATA_TO_ENCODE,
'Invalid data to encode. Data should be a valid ABI type. You need a valid type and valid data to encode.'
'Encoding failed: Data must be a valid ABI type with corresponding valid data.'
);
}
}
Expand All @@ -49,7 +49,7 @@ function decode<ReturnType>(
} catch {
throw buildError(
ABI.INVALID_DATA_TO_DECODE,
'Invalid data to decode. Data should be a valid hex string that encodes a valid ABI type.'
'Decoding failed: Data must be a valid hex string that encodes a valid ABI type'
);
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/abi/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function encodeFunctionInput(
} catch (e) {
throw buildError(
ERROR_CODES.ABI.CONTRACT_INTERFACE_ERROR,
'Cannot encode the input of the function',
'Encoding failed: Function input must match ABI specifications and be correctly formatted',
{ functionName, functionData },
e
);
Expand Down Expand Up @@ -61,7 +61,7 @@ function decodeFunctionInput(
} catch (e) {
throw buildError(
ERROR_CODES.ABI.CONTRACT_INTERFACE_ERROR,
'Cannot decode the input of the function',
'Decoding failed: Function input must be properly encoded per ABI specifications',
{ functionName },
e
);
Expand Down Expand Up @@ -89,7 +89,7 @@ function encodeEventLog(
} catch (e) {
throw buildError(
ERROR_CODES.ABI.CONTRACT_INTERFACE_ERROR,
'Cannot encode the input of the event log',
'Encoding failed: Event log data must align with ABI specifications for encoding',
{ eventName },
e
);
Expand Down Expand Up @@ -117,7 +117,7 @@ function decodeEventLog(
} catch (e) {
throw buildError(
ERROR_CODES.ABI.CONTRACT_INTERFACE_ERROR,
'Cannot decode the input of the event log',
'Decoding failed: Event log data must be correctly encoded per ABI specifications',
{ eventName },
e
);
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/abi/fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function getSignature(fragment: Fragment, formatType: FormatType): string {
assert(
allowedSignatureFormats.includes(formatType),
ABI.INVALID_FORMAT_TYPE,
`Invalid format type. Allowed formats are: ${allowedSignatureFormats.join(
`Signature format error: '${formatType}' is invalid. Allowed formats: ${allowedSignatureFormats.join(
', '
)}`,
{ formatType }
Expand Down Expand Up @@ -68,7 +68,7 @@ class Function<ABIType> {
} catch (e) {
throw buildError(
ABI.INVALID_FUNCTION,
'Invalid Function format. Cannot create Function fragment.',
'Initialization failed: Cannot create Function fragment. Function format is invalid',
{ source },
e
);
Expand Down Expand Up @@ -107,7 +107,7 @@ class Function<ABIType> {
} catch (e) {
throw buildError(
ABI.INVALID_DATA_TO_DECODE,
'Cannot decode. Data should be a valid hex string that encodes a valid ABI type.',
'Decoding failed: Data must be a valid hex string encoding a compliant ABI type.',
{ data },
e
);
Expand All @@ -127,7 +127,7 @@ class Function<ABIType> {
} catch (e) {
throw buildError(
ABI.INVALID_DATA_TO_ENCODE,
'Cannot encode. Incorrect Function format.',
'Encoding failed: Data format is invalid. Function data match the expected format for ABI type encoding.',
{ dataToEncode },
e
);
Expand Down Expand Up @@ -164,7 +164,7 @@ class Event<ABIType> {
} catch {
throw buildError(
ABI.INVALID_EVENT,
'Invalid Event format. Cannot create Event fragment.'
'Initialization failed: Event fragment creation not possible due to invalid ABI data format.'
);
}
}
Expand Down Expand Up @@ -205,7 +205,7 @@ class Event<ABIType> {
} catch {
throw buildError(
ABI.INVALID_DATA_TO_DECODE,
'Cannot decode. Incorrect data or topics.'
'Decoding failed: Data and topics must be correctly formatted for ABI-compliant decoding.'
);
}
}
Expand All @@ -226,7 +226,7 @@ class Event<ABIType> {
} catch {
throw buildError(
ABI.INVALID_DATA_TO_ENCODE,
'Cannot encode. Incorrect Event format.'
'Encoding failed: Event data must be correctly formatted for ABI-compliant encoding.'
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/address/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function toChecksumed(address: string): string {
assert(
isAddress(address),
ADDRESS.INVALID_ADDRESS,
'Invalid address given as input.',
'Checksum failed: Input must be a valid Vechain Thor address.',
{ address }
);

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/certificate/certificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function verify(cert: Certificate): void {
assert(
cert.signature !== undefined && cert.signature !== null,
CERTIFICATE.CERTIFICATE_NOT_SIGNED,
"Can't verify the certificate, signature not found",
"Verification failed: Certificate's signature is missing.",
{ cert }
);

Expand All @@ -40,7 +40,7 @@ function verify(cert: Certificate): void {
dataUtils.isHexString(cert.signature as string) &&
(cert.signature as string).length % 2 === 0,
CERTIFICATE.CERTIFICATE_INVALID_SIGNATURE_FORMAT,
'Invalid signature format.',
'Verification failed: Signature format is invalid.',
{ cert }
);

Expand All @@ -56,7 +56,7 @@ function verify(cert: Certificate): void {
assert(
addressUtils.fromPublicKey(pubKey) === cert.signer,
CERTIFICATE.CERTIFICATE_INVALID_SIGNER,
"Signature does not match with the signer's public key.",
"Verification failed: Signature does not correspond to the signer's public key.",
{ pubKey, cert }
);
}
Expand Down
15 changes: 10 additions & 5 deletions packages/core/src/encoding/rlp/helpers/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ function assertIsValidBuffer(
bufferToCheck: Buffer | RLPInput,
context: string
): void {
assert(Buffer.isBuffer(bufferToCheck), RLP.INVALID_RLP, 'expected buffer', {
bufferToCheck,
context
});
assert(
Buffer.isBuffer(bufferToCheck),
RLP.INVALID_RLP,
`Validation error: Expected a Buffer type in ${context}.`,
{
bufferToCheck,
context
}
);
}

/**
Expand All @@ -38,7 +43,7 @@ function assertIsArray<ArrayType>(
assert(
Array.isArray(arrayToCheck),
RLPError.INVALID_RLP,
'expected array',
`Validation error: Expected an array in ${context}.`,
{
context
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ const assertCompactFixedHexBlobBuffer = (
assert(
buffer.length <= bytes,
RLP.INVALID_RLP,
`expected buffer to be at most ${bytes} bytes`,
`Validation error: Buffer in ${context} must be at most ${bytes} bytes.`,
{ buffer, context }
);

assert(
buffer.length === 0 || buffer[0] !== 0,
RLP.INVALID_RLP,
'expected no leading zero bytes',
`Validation error: Buffer in ${context} should not have leading zero bytes.`,
{
buffer,
context
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/encoding/rlp/helpers/fixedhexblobkind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const assertFixedHexBlobKindData = (
assert(
data.length === bytes * 2 + 2,
RLP.INVALID_RLP,
`expected hex string to be ${bytes} bytes`,
`Validation error: Hex string in ${context} must be exactly ${bytes} bytes in length.`,
{ data, context }
);
};
Expand All @@ -38,7 +38,7 @@ const assertFixedHexBlobKindBuffer = (
assert(
buffer.length === bytes,
RLP.INVALID_RLP,
`expected buffer to be ${bytes} bytes`,
`Validation error: Buffer in ${context} must be exactly ${bytes} bytes in length.`,
{ buffer, context }
);
};
Expand Down
30 changes: 20 additions & 10 deletions packages/core/src/encoding/rlp/helpers/hexblobkind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,29 @@ import { assert, RLP } from '@vechainfoundation/vechain-sdk-errors';
* @param context - Additional context for error handling.
*/
const assertValidHexBlobKindData = (data: RLPInput, context: string): void => {
assert(typeof data === 'string', RLP.INVALID_RLP, 'expected string', {
data,
context
});
assert(
typeof data === 'string',
RLP.INVALID_RLP,
'Validation error: Input must be a string.',
{
data,
context
}
);

// Check if data is a valid hex string with '0x' prefix.
assert(
dataUtils.isHexString(data as string, true),
RLP.INVALID_RLP,
'expected hex string',
"Validation error: Input must be a valid hex string with a '0x' prefix.",
{ data, context }
);

// Ensure the hex string length is even.
assert(
(data as string).length % 2 === 0,
RLP.INVALID_RLP,
'expected even length string',
'Validation error: Hex string must have an even length.',
{ data, context }
);
};
Expand All @@ -43,10 +48,15 @@ const assertValidHexBlobKindBuffer = (
buffer: Buffer,
context: string
): void => {
assert(Buffer.isBuffer(buffer), RLP.INVALID_RLP, 'expected buffer', {
buffer,
context
});
assert(
Buffer.isBuffer(buffer),
RLP.INVALID_RLP,
'Validation error: Input must be a valid buffer.',
{
buffer,
context
}
);
};

export { assertValidHexBlobKindData, assertValidHexBlobKindBuffer };
14 changes: 7 additions & 7 deletions packages/core/src/encoding/rlp/helpers/numerickind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const validateNumericKindData = (data: RLPInput, context: string): bigint => {
assert(
typeof data === 'number' || typeof data === 'string',
RLP.INVALID_RLP,
'expected string or number',
`Validation error: Input in ${context} must be a string or number.`,
{ data, context }
);

Expand Down Expand Up @@ -45,7 +45,7 @@ const _validateNumericKindNumber = (num: number, context: string): void => {
assert(
Number.isSafeInteger(num) && num >= 0,
RLP.INVALID_RLP,
'expected integer',
`Validation error: Number in ${context} must be a safe and non-negative integer.`,
{
num,
context
Expand Down Expand Up @@ -73,15 +73,15 @@ const _validateNumericKindString = (str: string, context: string): void => {
assert(
isHex || isDecimal,
RLP.INVALID_RLP,
'expected non-negative integer in hex or dec string',
`Validation error: String in ${context} must represent a non-negative integer in hex or decimal format.`,
{ str, context }
);

// Ensure hex numbers are of a valid length.
assert(
!isHex || str.length > 2,
RLP.INVALID_RLP,
'expected valid hex string number',
`Validation error: Hex string number in ${context} must be of valid length.`,
{ str, context }
);
};
Expand All @@ -107,15 +107,15 @@ const assertValidNumericKindBuffer = (
assert(
maxBytes === undefined || buf.length <= maxBytes,
RLP.INVALID_RLP,
`expected less than ${maxBytes} bytes`,
`Validation error: Buffer in ${context} must be less than ${maxBytes} bytes.`,
{ maxBytes, context }
);

// Ensure the buffer does not have leading zeros, as it's not canonical in integer representation.
assert(
buf[0] !== 0,
RLP.INVALID_RLP,
'expected canonical integer (no leading zero bytes)',
`Validation error: Buffer in ${context} must represent a canonical integer (no leading zeros).`,
{ buf, context }
);
};
Expand Down Expand Up @@ -146,7 +146,7 @@ const encodeBigIntToBuffer = (
assert(
maxBytes === undefined || hex.length <= maxBytes * 2,
RLP.INVALID_RLP,
`expected number in ${maxBytes} bytes`,
`Validation error: Encoded number in ${context} must fit within ${maxBytes} bytes.`,
{ maxBytes, hex, context }
);

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/encoding/rlp/rlp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ const _unpackData = (
assert(
Buffer.isBuffer(packed) || packed instanceof Uint8Array,
RLPError.INVALID_RLP,
'expected buffer',
'Unpacking error: Expected data type is Buffer.',
{ context }
);

Expand All @@ -151,7 +151,7 @@ const _unpackData = (
assert(
parts.length === kind.length,
RLPError.INVALID_RLP,
`expected ${kind.length} items, but got ${parts.length}`,
`Unpacking error: Expected ${kind.length} items, but got ${parts.length}.`,
{ context }
);

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/hash/helpers/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function assertIsValidReturnType(returnType: ReturnType): void {
assert(
isValidReturnType(returnType),
DATA.INVALID_DATA_RETURN_TYPE,
"Invalid return type. Return type should be either 'buffer' or 'hex'",
"Validation error: Invalid Return type. Return type in hash function must be 'buffer' or 'hex'.",
{ returnType }
);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/hdnode/hdnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function fromMnemonic(words: string[], path = VET_DERIVATION_PATH): IHDNode {
words.length as WordlistSizeType
),
HDNODE.INVALID_HDNODE_MNEMONICS,
'Invalid mnemonic size. It must be 12, 15, 18, 21, or 24.',
'Invalid mnemonic size. Mnemonic must be 12, 15, 18, 21, or 24 words.',
{ words }
);

Expand Down Expand Up @@ -60,7 +60,7 @@ function fromPublicKey(publicKey: Buffer, chainCode: Buffer): IHDNode {
assert(
publicKey.length === 65,
HDNODE.INVALID_HDNODE_PUBLIC_KEY,
'Invalid public key. Length must be 65 bytes.',
'Invalid public key. Length must be exactly 65 bytes.',
{ publicKey }
);

Expand Down Expand Up @@ -95,7 +95,7 @@ function fromPrivateKey(privateKey: Buffer, chainCode: Buffer): IHDNode {
assert(
privateKey.length === 32,
HDNODE.INVALID_HDNODE_PRIVATE_KEY,
'Invalid private key. Length must be 32 bytes.',
'Invalid private key. Length must be exactly 32 bytes.',
{ privateKey }
);

Expand Down
Loading

1 comment on commit 6492cfb

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Coverage

Summary

Lines Statements Branches Functions
Coverage: 100%
100% (1244/1244) 100% (273/273) 100% (263/263)
Title Tests Skipped Failures Errors Time
core 347 0 💤 0 ❌ 0 🔥 1m 9s ⏱️
network 90 0 💤 0 ❌ 0 🔥 1m 25s ⏱️
errors 30 0 💤 0 ❌ 0 🔥 8.099s ⏱️

Please sign in to comment.