From 6492cfb0c342a26da9e25863e65a10a9db36b2c7 Mon Sep 17 00:00:00 2001 From: Antonio Senatore Date: Fri, 1 Dec 2023 16:03:31 +0000 Subject: [PATCH] refactor: made error messages more consistent (#330) Co-authored-by: fabiorigam --- packages/core/src/abi/coder.ts | 4 +-- packages/core/src/abi/contract.ts | 8 ++--- packages/core/src/abi/fragment.ts | 14 ++++----- packages/core/src/address/address.ts | 2 +- packages/core/src/certificate/certificate.ts | 6 ++-- .../src/encoding/rlp/helpers/assertions.ts | 15 ++++++---- .../rlp/helpers/compactfixedhexblobkind.ts | 4 +-- .../encoding/rlp/helpers/fixedhexblobkind.ts | 4 +-- .../src/encoding/rlp/helpers/hexblobkind.ts | 30 ++++++++++++------- .../src/encoding/rlp/helpers/numerickind.ts | 14 ++++----- packages/core/src/encoding/rlp/rlp.ts | 4 +-- packages/core/src/hash/helpers/assertions.ts | 2 +- packages/core/src/hdnode/hdnode.ts | 6 ++-- .../core/src/hdnode/helpers/assertions.ts | 4 +-- .../core/src/keystore/helpers/assertions.ts | 4 +-- packages/core/src/keystore/keystore.ts | 13 +++++--- packages/core/src/mnemonic/mnemonic.ts | 2 +- packages/core/src/secp256k1/secp256k1.ts | 4 +-- .../core/src/transaction/handlers/decode.ts | 2 +- .../core/src/transaction/handlers/sign.ts | 2 +- .../src/transaction/helpers/assertions.ts | 2 +- packages/core/src/transaction/transaction.ts | 8 ++--- packages/core/src/utils/bloom/bloom.ts | 2 +- packages/core/src/utils/data/data.ts | 2 +- .../core/src/utils/transaction/transaction.ts | 4 +-- packages/core/src/utils/units/units.ts | 2 +- packages/errors/tests/errorHandler.test.ts | 14 +++++---- .../transactions/transactions-client.ts | 5 ++-- .../network/src/utils/http/http-client.ts | 2 +- .../src/utils/poll/helpers/assertions.ts | 2 +- packages/network/src/utils/poll/sync.ts | 2 +- packages/network/tests/utils/poll/fixture.ts | 5 +++- 32 files changed, 111 insertions(+), 83 deletions(-) diff --git a/packages/core/src/abi/coder.ts b/packages/core/src/abi/coder.ts index 06a1b238d..f80576446 100644 --- a/packages/core/src/abi/coder.ts +++ b/packages/core/src/abi/coder.ts @@ -24,7 +24,7 @@ function encode(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.' ); } } @@ -49,7 +49,7 @@ function decode( } 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' ); } } diff --git a/packages/core/src/abi/contract.ts b/packages/core/src/abi/contract.ts index 9ee6bf703..796a3d3b4 100644 --- a/packages/core/src/abi/contract.ts +++ b/packages/core/src/abi/contract.ts @@ -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 ); @@ -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 ); @@ -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 ); @@ -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 ); diff --git a/packages/core/src/abi/fragment.ts b/packages/core/src/abi/fragment.ts index d6edd11de..0deb06f25 100644 --- a/packages/core/src/abi/fragment.ts +++ b/packages/core/src/abi/fragment.ts @@ -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 } @@ -68,7 +68,7 @@ class Function { } 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 ); @@ -107,7 +107,7 @@ class Function { } 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 ); @@ -127,7 +127,7 @@ class Function { } 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 ); @@ -164,7 +164,7 @@ class Event { } 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.' ); } } @@ -205,7 +205,7 @@ class Event { } 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.' ); } } @@ -226,7 +226,7 @@ class Event { } 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.' ); } } diff --git a/packages/core/src/address/address.ts b/packages/core/src/address/address.ts index 3a53ebc97..1f459e454 100644 --- a/packages/core/src/address/address.ts +++ b/packages/core/src/address/address.ts @@ -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 } ); diff --git a/packages/core/src/certificate/certificate.ts b/packages/core/src/certificate/certificate.ts index 9a0f7824a..a1e81474f 100644 --- a/packages/core/src/certificate/certificate.ts +++ b/packages/core/src/certificate/certificate.ts @@ -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 } ); @@ -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 } ); @@ -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 } ); } diff --git a/packages/core/src/encoding/rlp/helpers/assertions.ts b/packages/core/src/encoding/rlp/helpers/assertions.ts index f088f37c0..5435348b9 100644 --- a/packages/core/src/encoding/rlp/helpers/assertions.ts +++ b/packages/core/src/encoding/rlp/helpers/assertions.ts @@ -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 + } + ); } /** @@ -38,7 +43,7 @@ function assertIsArray( assert( Array.isArray(arrayToCheck), RLPError.INVALID_RLP, - 'expected array', + `Validation error: Expected an array in ${context}.`, { context } diff --git a/packages/core/src/encoding/rlp/helpers/compactfixedhexblobkind.ts b/packages/core/src/encoding/rlp/helpers/compactfixedhexblobkind.ts index c1620144d..07b4b64cd 100644 --- a/packages/core/src/encoding/rlp/helpers/compactfixedhexblobkind.ts +++ b/packages/core/src/encoding/rlp/helpers/compactfixedhexblobkind.ts @@ -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 diff --git a/packages/core/src/encoding/rlp/helpers/fixedhexblobkind.ts b/packages/core/src/encoding/rlp/helpers/fixedhexblobkind.ts index 50decdabf..89a45afda 100644 --- a/packages/core/src/encoding/rlp/helpers/fixedhexblobkind.ts +++ b/packages/core/src/encoding/rlp/helpers/fixedhexblobkind.ts @@ -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 } ); }; @@ -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 } ); }; diff --git a/packages/core/src/encoding/rlp/helpers/hexblobkind.ts b/packages/core/src/encoding/rlp/helpers/hexblobkind.ts index 25585228f..84092e930 100644 --- a/packages/core/src/encoding/rlp/helpers/hexblobkind.ts +++ b/packages/core/src/encoding/rlp/helpers/hexblobkind.ts @@ -10,16 +10,21 @@ 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 } ); @@ -27,7 +32,7 @@ const assertValidHexBlobKindData = (data: RLPInput, context: string): void => { 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 } ); }; @@ -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 }; diff --git a/packages/core/src/encoding/rlp/helpers/numerickind.ts b/packages/core/src/encoding/rlp/helpers/numerickind.ts index 4b73e2b06..e2ebe5494 100644 --- a/packages/core/src/encoding/rlp/helpers/numerickind.ts +++ b/packages/core/src/encoding/rlp/helpers/numerickind.ts @@ -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 } ); @@ -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 @@ -73,7 +73,7 @@ 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 } ); @@ -81,7 +81,7 @@ const _validateNumericKindString = (str: string, context: string): void => { 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 } ); }; @@ -107,7 +107,7 @@ 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 } ); @@ -115,7 +115,7 @@ const assertValidNumericKindBuffer = ( 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 } ); }; @@ -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 } ); diff --git a/packages/core/src/encoding/rlp/rlp.ts b/packages/core/src/encoding/rlp/rlp.ts index d7f46a619..250d31a72 100644 --- a/packages/core/src/encoding/rlp/rlp.ts +++ b/packages/core/src/encoding/rlp/rlp.ts @@ -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 } ); @@ -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 } ); diff --git a/packages/core/src/hash/helpers/assertions.ts b/packages/core/src/hash/helpers/assertions.ts index ba362b8e8..6251c5569 100644 --- a/packages/core/src/hash/helpers/assertions.ts +++ b/packages/core/src/hash/helpers/assertions.ts @@ -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 } ); } diff --git a/packages/core/src/hdnode/hdnode.ts b/packages/core/src/hdnode/hdnode.ts index 60aaba28a..67e4bb773 100644 --- a/packages/core/src/hdnode/hdnode.ts +++ b/packages/core/src/hdnode/hdnode.ts @@ -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 } ); @@ -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 } ); @@ -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 } ); diff --git a/packages/core/src/hdnode/helpers/assertions.ts b/packages/core/src/hdnode/helpers/assertions.ts index ec758254e..ea1b9d893 100644 --- a/packages/core/src/hdnode/helpers/assertions.ts +++ b/packages/core/src/hdnode/helpers/assertions.ts @@ -10,7 +10,7 @@ function assertIsValidHdNodeDerivationPath(path: string): void { assert( isDerivationPathValid(path), HDNODE.INVALID_HDNODE_DERIVATION_PATH, - 'Invalid derivation path.', + 'Invalid derivation path. Ensure the path adheres to the standard format.', { path } ); } @@ -24,7 +24,7 @@ function assertIsValidHdNodeChainCode(chainCode: Buffer): void { assert( chainCode.length === 32, HDNODE.INVALID_HDNODE_CHAIN_CODE, - 'Invalid chain code. Length must be 32 bytes.', + 'Invalid chain code. Length must be exactly 32 bytes.', { chainCode } ); } diff --git a/packages/core/src/keystore/helpers/assertions.ts b/packages/core/src/keystore/helpers/assertions.ts index 01f8b04a2..df6b89d7c 100644 --- a/packages/core/src/keystore/helpers/assertions.ts +++ b/packages/core/src/keystore/helpers/assertions.ts @@ -13,7 +13,7 @@ function assertIsValidPrivateKey( assert( isValidPrivateKeyFunction(privateKey), SECP256K1.INVALID_SECP256k1_PRIVATE_KEY, - 'Invalid private key given as input. Length must be 32 bytes', + 'Invalid private key given as input. Ensure it is a valid 32-byte SECP256k1 private key.', { privateKey } ); } @@ -31,7 +31,7 @@ function assertIsValidSecp256k1MessageHash( assert( isValidMessageHashFunction(msgHash), SECP256K1.INVALID_SECP256k1_MESSAGE_HASH, - 'Invalid message hash given as input. Length must be 32 bytes', + 'Invalid private key given as input. Ensure it is a valid 32-byte SECP256k1 private key.', { msgHash } ); } diff --git a/packages/core/src/keystore/keystore.ts b/packages/core/src/keystore/keystore.ts index d8d42e7f7..43dce3640 100644 --- a/packages/core/src/keystore/keystore.ts +++ b/packages/core/src/keystore/keystore.ts @@ -65,9 +65,14 @@ async function decrypt( password: string ): Promise { // Invalid keystore - assert(isValid(keystore), KEYSTORE.INVALID_KEYSTORE, 'Invalid keystore', { - keystore - }); + assert( + isValid(keystore), + KEYSTORE.INVALID_KEYSTORE, + 'Invalid keystore. Ensure the keystore is properly formatted and contains the necessary data.', + { + keystore + } + ); try { return (await ethers.decryptKeystoreJson( @@ -77,7 +82,7 @@ async function decrypt( } catch (e) { throw buildError( KEYSTORE.INVALID_PASSWORD, - 'Invalid password', + 'Decryption failed: Invalid Password for the given keystore.', { keystore, password diff --git a/packages/core/src/mnemonic/mnemonic.ts b/packages/core/src/mnemonic/mnemonic.ts index 8f6ea07b9..543b3198d 100644 --- a/packages/core/src/mnemonic/mnemonic.ts +++ b/packages/core/src/mnemonic/mnemonic.ts @@ -56,7 +56,7 @@ function generate( wordlistSize === undefined || MNEMONIC_WORDLIST_ALLOWED_SIZES.includes(wordlistSize), HDNODE.INVALID_HDNODE_MNEMONICS, - 'Invalid wordlist size given as input. Allowed sizes are 12, 15, 18, 21, 24', + 'Invalid wordlist size given as input. Allowed sizes are 12, 15, 18, 21, 24 words.', { wordlistSize } ); diff --git a/packages/core/src/secp256k1/secp256k1.ts b/packages/core/src/secp256k1/secp256k1.ts index 413ed58a3..131b64655 100644 --- a/packages/core/src/secp256k1/secp256k1.ts +++ b/packages/core/src/secp256k1/secp256k1.ts @@ -93,7 +93,7 @@ function recover(messageHash: Buffer, sig: Buffer): Buffer { assert( Buffer.isBuffer(sig) && sig.length === SIGNATURE_LENGTH, SECP256K1.INVALID_SECP256k1_SIGNATURE, - 'Invalid signature given as input. Length must be 65 bytes', + 'Invalid signature given as input. Length must be exactly 65 bytes.', { sig } ); @@ -101,7 +101,7 @@ function recover(messageHash: Buffer, sig: Buffer): Buffer { assert( recovery === 0 || recovery === 1, SECP256K1.INVALID_SECP256k1_SIGNATURE_RECOVERY, - 'Invalid signature recovery given as input. Signature bytes in position 64 must be 0 or 1', + 'Invalid signature recovery value. Signature bytes at position 64 must be 0 or 1.', { recovery } ); diff --git a/packages/core/src/transaction/handlers/decode.ts b/packages/core/src/transaction/handlers/decode.ts index 11edbd62e..f8e9a93e5 100644 --- a/packages/core/src/transaction/handlers/decode.ts +++ b/packages/core/src/transaction/handlers/decode.ts @@ -78,7 +78,7 @@ function _decodeReservedField(reserved: Buffer[]): { assert( reserved[reserved.length - 1].length !== 0, TRANSACTION.INVALID_TRANSACTION_BODY, - 'Invalid reserved field. Fields must be trimmed', + 'Invalid reserved field. Fields in the reserved buffer must be properly trimmed.', { reserved } ); diff --git a/packages/core/src/transaction/handlers/sign.ts b/packages/core/src/transaction/handlers/sign.ts index b47f298f4..811cad3d7 100644 --- a/packages/core/src/transaction/handlers/sign.ts +++ b/packages/core/src/transaction/handlers/sign.ts @@ -79,7 +79,7 @@ function signWithDelegator( assert( transactionToSign.isDelegated, TRANSACTION.INVALID_DELEGATION, - 'Transaction is not delegated. You are using the wrong method. Use sign method instead.', + 'Transaction is not delegated. Use sign method instead.', { transactionToSign } ); diff --git a/packages/core/src/transaction/helpers/assertions.ts b/packages/core/src/transaction/helpers/assertions.ts index 6434deb1e..6d86ba418 100644 --- a/packages/core/src/transaction/helpers/assertions.ts +++ b/packages/core/src/transaction/helpers/assertions.ts @@ -20,7 +20,7 @@ function assertIsValidTransactionSigningPrivateKey( assert( isValidPrivateKeyFunction(privateKey), SECP256K1.INVALID_SECP256k1_PRIVATE_KEY, - `Invalid ${role} private key used to sign the transaction.`, + `Invalid ${role} private key used to sign the transaction. Ensure it's a valid SECP256k1 private key.`, { privateKey } ); } diff --git a/packages/core/src/transaction/transaction.ts b/packages/core/src/transaction/transaction.ts index e36634978..e99806270 100644 --- a/packages/core/src/transaction/transaction.ts +++ b/packages/core/src/transaction/transaction.ts @@ -57,7 +57,7 @@ class Transaction { assert( this._isValidBody(body), TRANSACTION.INVALID_TRANSACTION_BODY, - 'Invalid transaction body', + 'Invalid transaction body. Ensure all required fields are correctly formatted and present.', { body } ); this.body = body; @@ -67,7 +67,7 @@ class Transaction { assert( this._isSignatureValid(signature), SECP256K1.INVALID_SECP256k1_SIGNATURE, - 'Invalid transaction signature', + 'Invalid transaction signature. Ensure it is correctly formatted.', { signature } ); @@ -106,7 +106,7 @@ class Transaction { assert( this.isDelegated, TRANSACTION.INVALID_DELEGATION, - 'Transaction is not delegated' + 'Transaction is not delegated. Delegator information is unavailable.' ); // Unsigned transaction (@note we don't check if signature is valid or not, because we have checked it into constructor at creation time) @@ -191,7 +191,7 @@ class Transaction { assert( delegateFor === undefined || addressUtils.isAddress(delegateFor), ADDRESS.INVALID_ADDRESS, - 'Invalid address given as input as delegateFor parameter.', + 'Invalid address given as input as delegateFor parameter. Ensure it is a valid address.', { delegateFor } ); diff --git a/packages/core/src/utils/bloom/bloom.ts b/packages/core/src/utils/bloom/bloom.ts index 359be4610..bd1dd93e5 100644 --- a/packages/core/src/utils/bloom/bloom.ts +++ b/packages/core/src/utils/bloom/bloom.ts @@ -115,7 +115,7 @@ const isAddressInBloom = ( assert( addressUtils.isAddress(addressToCheck), ADDRESS.INVALID_ADDRESS, - 'Invalid address given as input in Bloom filter.', + 'Invalid address given as input in Bloom filter. Ensure it is a valid vechain thor address.', { addressToCheck } ); diff --git a/packages/core/src/utils/data/data.ts b/packages/core/src/utils/data/data.ts index 888b648b4..79ef278d8 100644 --- a/packages/core/src/utils/data/data.ts +++ b/packages/core/src/utils/data/data.ts @@ -130,7 +130,7 @@ const encodeBytes32String = ( } catch (e) { throw buildError( DATA.INVALID_DATA_TYPE, - `Failed to encode value ${value} to bytes32 string`, + `Encoding to bytes32 failed: Value '${value}' exceeds 32 bytes or is otherwise invalid.`, { value, zeroPadding }, e ); diff --git a/packages/core/src/utils/transaction/transaction.ts b/packages/core/src/utils/transaction/transaction.ts index c275a677a..b800dbf54 100644 --- a/packages/core/src/utils/transaction/transaction.ts +++ b/packages/core/src/utils/transaction/transaction.ts @@ -29,7 +29,7 @@ function intrinsicGas(clauses: TransactionClause[]): number { assert( addressUtils.isAddress(clause.to), DATA.INVALID_DATA_TYPE, - 'Invalid data type. Data should be an address.', + `Invalid data type in clause. Each 'to' field must be a valid address.`, { clause } ); @@ -54,7 +54,7 @@ function _calculateDataUsedGas(data: string): number { assert( data === '' || dataUtils.isHexString(data), DATA.INVALID_DATA_TYPE, - 'Invalid data type. Data should be an hexadecimal string.', + 'Invalid data type for gas calculation. Data should be a hexadecimal string.', { data } ); diff --git a/packages/core/src/utils/units/units.ts b/packages/core/src/utils/units/units.ts index a3776eb58..83de96e11 100644 --- a/packages/core/src/utils/units/units.ts +++ b/packages/core/src/utils/units/units.ts @@ -23,7 +23,7 @@ const parseUnits = ( assert( typeof value !== 'string' || dataUtils.isNumeric(value), DATA.INVALID_DATA_TYPE, - `The value "${value}" is not a valid hexadecimal string.`, + `Invalid value format. The value "${value}" must be a numeric string.`, { value } ); diff --git a/packages/errors/tests/errorHandler.test.ts b/packages/errors/tests/errorHandler.test.ts index 2baf2d48b..11a0a2799 100644 --- a/packages/errors/tests/errorHandler.test.ts +++ b/packages/errors/tests/errorHandler.test.ts @@ -20,7 +20,7 @@ describe('Error handler test', () => { expect(() => { throw buildError( ERROR_CODES.KEYSTORE.INVALID_KEYSTORE, - 'Invalid Keystore' + 'Invalid Keystore: Missing or malformed data' ); }).toThrowError(InvalidKeystoreError); }); @@ -31,7 +31,7 @@ describe('Error handler test', () => { try { throw buildError( ERROR_CODES.KEYSTORE.INVALID_KEYSTORE, - 'Invalid Keystore', + 'Invalid Keystore: Missing or malformed data', { test: 'test' } ); } catch (error) { @@ -45,9 +45,13 @@ describe('Error handler test', () => { */ test('Throw Invalid RLP Exception with data', () => { try { - throw buildError(ERROR_CODES.RLP.INVALID_RLP, 'Invalid Keystore', { - context: 'test' - }); + throw buildError( + ERROR_CODES.RLP.INVALID_RLP, + 'Invalid Keystore: Missing or malformed data', + { + context: 'test' + } + ); } catch (error) { const InvalidRLPErrorObject = error as InvalidRLPError; expect(InvalidRLPErrorObject.data?.context).toBeDefined(); diff --git a/packages/network/src/clients/thorest-client/transactions/transactions-client.ts b/packages/network/src/clients/thorest-client/transactions/transactions-client.ts index 3884087c2..c20dacb6f 100644 --- a/packages/network/src/clients/thorest-client/transactions/transactions-client.ts +++ b/packages/network/src/clients/thorest-client/transactions/transactions-client.ts @@ -100,7 +100,7 @@ class TransactionsClient { assert( dataUtils.isHexString(raw), DATA.INVALID_DATA_TYPE, - 'Invalid raw transaction given as input. Input must be an hex string', + 'Sending failed: Input must be a valid raw transaction in hex format.', { raw } ); @@ -110,7 +110,8 @@ class TransactionsClient { } catch (error) { throw buildError( DATA.INVALID_DATA_TYPE, - 'Invalid raw transaction given as input. Input must be a valid raw transaction. Error occurs while decoding the transaction.', + 'Sending failed: Input must be a valid raw transaction in hex format. Decoding error encountered.', + { raw }, error ); diff --git a/packages/network/src/utils/http/http-client.ts b/packages/network/src/utils/http/http-client.ts index e6c50544d..898c9dfcc 100644 --- a/packages/network/src/utils/http/http-client.ts +++ b/packages/network/src/utils/http/http-client.ts @@ -70,7 +70,7 @@ class HttpClient { // If it's not an Axios error, re-throw the original error throw buildError( HTTP_CLIENT.INVALID_HTTP_REQUEST, - `An error occurred while performing http request`, + 'HTTP request failed: Check method, path, and parameters for validity.', { method, path, params }, err ); diff --git a/packages/network/src/utils/poll/helpers/assertions.ts b/packages/network/src/utils/poll/helpers/assertions.ts index af5722e08..ed77fe7a9 100644 --- a/packages/network/src/utils/poll/helpers/assertions.ts +++ b/packages/network/src/utils/poll/helpers/assertions.ts @@ -13,7 +13,7 @@ function assertPositiveIntegerForPollOptions( assert( field === undefined || (field > 0 && Number.isInteger(field)), DATA.INVALID_DATA_TYPE, - `${fieldName} must be a positive number`, + `Invalid input for fieldname. ${fieldName} must be a positive number`, { field } ); } diff --git a/packages/network/src/utils/poll/sync.ts b/packages/network/src/utils/poll/sync.ts index dbdb1e620..aa46888b8 100644 --- a/packages/network/src/utils/poll/sync.ts +++ b/packages/network/src/utils/poll/sync.ts @@ -121,7 +121,7 @@ function SyncPoll( } catch (error) { throw buildError( POLL_ERROR.POLL_EXECUTION_ERROR, - 'Error on function execution', + 'Polling failed: Function execution error encountered during synchronous polling.', { functionName: pollingFunction.name }, diff --git a/packages/network/tests/utils/poll/fixture.ts b/packages/network/tests/utils/poll/fixture.ts index 459b53f0a..620554af9 100644 --- a/packages/network/tests/utils/poll/fixture.ts +++ b/packages/network/tests/utils/poll/fixture.ts @@ -24,7 +24,10 @@ const simpleThrowErrorFunctionIfInputIs10 = async ( a: number ): Promise => { if (a === 10) - throw buildError(DATA.INVALID_DATA_RETURN_TYPE, 'a cannot be 10'); + throw buildError( + DATA.INVALID_DATA_RETURN_TYPE, + "Input value error: 'a' must not be 10" + ); return await simpleIncrementFunction(a, a + 1); };