From 0be814e111b0f29c13a4f41031c55d39140c4f8f Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Thu, 4 Apr 2024 07:18:45 +0100 Subject: [PATCH] chore: remove redundant error codes (#2002) * chore: Remove redundant error handling code (FRAGMENT_NOT_FOUND) * chore: Remove redundant error handling code (INSUFFICIENT_BALANCE) * chore: fix block terminator * chore: Remove redundant error handling code (INVALID_URL) * chore: Remove redundant error handling code (LATEST_BLOCK_UNAVAILABLE) * chore: Remove redundant error handling code (UNEXPECTED_HEX_VALUE) * chore: changeset * chore: fix changeset lol * chore: changeset * chore: remove redundant FuelError import * chore: lint * chore: removed hexlifyWithPrefix --- .changeset/slow-dancers-relax.md | 7 +++++++ packages/abi-coder/src/Interface.ts | 8 -------- packages/contract/src/contract-factory.ts | 4 ++-- packages/contract/src/util.ts | 16 +++------------ packages/errors/README.md | 25 ++++++++++++----------- packages/errors/src/error-codes.ts | 5 ----- packages/errors/src/fuel-error.test.ts | 4 ++-- 7 files changed, 27 insertions(+), 42 deletions(-) create mode 100644 .changeset/slow-dancers-relax.md diff --git a/.changeset/slow-dancers-relax.md b/.changeset/slow-dancers-relax.md new file mode 100644 index 00000000000..afa7e2e162c --- /dev/null +++ b/.changeset/slow-dancers-relax.md @@ -0,0 +1,7 @@ +--- +"@fuel-ts/abi-coder": minor +"@fuel-ts/contract": minor +"@fuel-ts/errors": minor +--- + +chore: remove redundant error codes \ No newline at end of file diff --git a/packages/abi-coder/src/Interface.ts b/packages/abi-coder/src/Interface.ts index 45868a1b0ed..7e3ddb3fdb5 100644 --- a/packages/abi-coder/src/Interface.ts +++ b/packages/abi-coder/src/Interface.ts @@ -52,10 +52,6 @@ export class Interface { const fragment = typeof functionFragment === 'string' ? this.getFunction(functionFragment) : functionFragment; - if (!fragment) { - throw new FuelError(ErrorCode.FRAGMENT_NOT_FOUND, 'Fragment not found.'); - } - return fragment.decodeArguments(data); } @@ -67,10 +63,6 @@ export class Interface { const fragment = typeof functionFragment === 'string' ? this.getFunction(functionFragment) : functionFragment; - if (!fragment) { - throw new FuelError(ErrorCode.FRAGMENT_NOT_FOUND, 'Fragment not found.'); - } - return fragment.encodeArguments(values, offset); } diff --git a/packages/contract/src/contract-factory.ts b/packages/contract/src/contract-factory.ts index f8a31ac8301..83ed38d744c 100644 --- a/packages/contract/src/contract-factory.ts +++ b/packages/contract/src/contract-factory.ts @@ -92,8 +92,8 @@ export default class ContractFactory { createTransactionRequest(deployContractOptions?: DeployContractOptions) { const storageSlots = deployContractOptions?.storageSlots ?.map(({ key, value }) => ({ - key: hexlifyWithPrefix(key, true), - value: hexlifyWithPrefix(value, true), + key: hexlifyWithPrefix(key), + value: hexlifyWithPrefix(value), })) .sort(({ key: keyA }, { key: keyB }) => keyA.localeCompare(keyB)); diff --git a/packages/contract/src/util.ts b/packages/contract/src/util.ts index b33901892bd..610f7b73ff1 100644 --- a/packages/contract/src/util.ts +++ b/packages/contract/src/util.ts @@ -1,4 +1,3 @@ -import { FuelError } from '@fuel-ts/errors'; import { sha256 } from '@fuel-ts/hasher'; import type { BytesLike } from '@fuel-ts/interfaces'; import { calcRoot, SparseMerkleTree } from '@fuel-ts/merkle'; @@ -64,17 +63,8 @@ export const getContractId = ( * Ensures that a string is hexlified. * * @param value - The value to be hexlified. - * @param isKnownHex - Required if using hex values that need to be converted + * * @returns The input value hexlified with prefix. */ -export const hexlifyWithPrefix = (value: string, isKnownHex = false) => { - if (value.startsWith('0x')) { - return hexlify(value); - } - - if (isKnownHex) { - return hexlify(`0x${value}`); - } - - throw new FuelError(FuelError.CODES.UNEXPECTED_HEX_VALUE, `Value should be hex string ${value}.`); -}; +export const hexlifyWithPrefix = (value: string) => + hexlify(value.startsWith('0x') ? value : `0x${value}`); diff --git a/packages/errors/README.md b/packages/errors/README.md index acb3452beab..fc4bc044477 100644 --- a/packages/errors/README.md +++ b/packages/errors/README.md @@ -38,11 +38,11 @@ npm add @fuel-ts/errors import { FuelError, ErrorCodes } from "@fuel-ts/error"; export function singleImport() { - throw new FuelError(FuelError.CODES.INVALID_URL, "Invalid URL"); + throw new FuelError(FuelError.CODES.INVALID_DATA, "Invalid data"); } export function multipleImports() { - throw new FuelError(ErrorCodes.INVALID_URL, "Invalid URL"); + throw new FuelError(ErrorCodes.INVALID_DATA, "Invalid data"); } ``` @@ -57,7 +57,7 @@ import { expectToThrowFuelError } from "@fuel-ts/errors"; import { myFn } from "..."; describe('this and that' () => { - const code = FuelError.CODES.INVALID_URL; + const code = FuelError.CODES.INVALID_DATA; it("should throw FuelError", async () => { const expected = new FuelError(code); @@ -77,18 +77,19 @@ describe('this and that' () => { ```ts import { FuelError, Provider } from "fuels"; -type Locale = "PT_BR" | "BS_BA"; +type Locale = "pt-BR" | "bs-BA" | "en-GB"; -const currentLocale: Locale = "PT_BR"; +const currentLocale: Locale = "pt-BR"; const i18nDict = { - PT_BR: { - [FuelError.CODES.INVALID_URL]: "Endereço inválido", - [FuelError.CODES.INSUFFICIENT_BALANCE]: "Saldo insuficiente", + pt-BR: { + [FuelError.CODES.INVALID_DATA]: "Dados inválidos", }, - BS_BA: { - [FuelError.CODES.INVALID_URL]: "Nevažeća adresa", - [FuelError.CODES.INSUFFICIENT_BALANCE]: "Nedovoljan balans", + bs-BA: { + [FuelError.CODES.INVALID_DATA]: "Nevažeći podaci", + }, + en-GB: { + [FuelError.CODES.INVALID_DATA]: "Invalid data", }, }; @@ -105,7 +106,7 @@ function main() { const prettyError = translateError(e); console.log({ prettyError }); } -); +}; ``` ## Contributing diff --git a/packages/errors/src/error-codes.ts b/packages/errors/src/error-codes.ts index a06ba311dc9..5c4a4f1d893 100644 --- a/packages/errors/src/error-codes.ts +++ b/packages/errors/src/error-codes.ts @@ -7,7 +7,6 @@ export enum ErrorCode { ABI_TYPES_AND_VALUES_MISMATCH = 'abi-types-and-values-mismatch', ABI_MAIN_METHOD_MISSING = 'abi-main-method-missing', INVALID_COMPONENT = 'invalid-component', - FRAGMENT_NOT_FOUND = 'fragment-not-found', CONFIGURABLE_NOT_FOUND = 'configurable-not-found', TYPE_NOT_FOUND = 'type-not-found', TYPE_NOT_SUPPORTED = 'type-not-supported', @@ -26,7 +25,6 @@ export enum ErrorCode { INVALID_B256_ADDRESS = 'invalid-b256-address', // provider - INVALID_URL = 'invalid-url', CHAIN_INFO_CACHE_EMPTY = 'chain-info-cache-empty', NODE_INFO_CACHE_EMPTY = 'node-info-cache-empty', MISSING_PROVIDER = 'missing-provider', @@ -35,7 +33,6 @@ export enum ErrorCode { // wallet INVALID_PUBLIC_KEY = 'invalid-public-key', - INSUFFICIENT_BALANCE = 'insufficient-balance', WALLET_MANAGER_ERROR = 'wallet-manager-error', HD_WALLET_ERROR = 'hd-wallet-error', MISSING_CONNECTOR = 'missing-connector', @@ -54,7 +51,6 @@ export enum ErrorCode { ELEMENT_NOT_FOUND = 'element-not-found', MISSING_REQUIRED_PARAMETER = 'missing-required-parameter', INVALID_REQUEST = 'invalid-request', - UNEXPECTED_HEX_VALUE = 'unexpected-hex-value', INVALID_TRANSFER_AMOUNT = 'invalid-transfer-amount', // transaction @@ -88,7 +84,6 @@ export enum ErrorCode { UNLOCKED_WALLET_REQUIRED = 'unlocked-wallet-required', // chain - LATEST_BLOCK_UNAVAILABLE = 'latest-block-unavailable', ERROR_BUILDING_BLOCK_EXPLORER_URL = 'error-building-block-explorer-url', UNSUPPORTED_FUEL_CLIENT_VERSION = 'unsupported-fuel-client-version', diff --git a/packages/errors/src/fuel-error.test.ts b/packages/errors/src/fuel-error.test.ts index 33e73fd5e24..d919f531449 100644 --- a/packages/errors/src/fuel-error.test.ts +++ b/packages/errors/src/fuel-error.test.ts @@ -20,10 +20,10 @@ it('has properties set as expected on creation', () => { describe('Parsing', () => { it('parses correctly', () => { const message = 'my-message'; - const error = FuelError.parse({ code: ErrorCode.INVALID_URL, message }); + const error = FuelError.parse({ code: ErrorCode.INVALID_DATA, message }); expect(error).toBeInstanceOf(FuelError); expect(error.message).toBe(message); - expect(error.code).toBe(ErrorCode.INVALID_URL); + expect(error.code).toBe(ErrorCode.INVALID_DATA); }); it('fails when parsing an object without a code property', async () => {