Skip to content

Commit

Permalink
chore: remove redundant error codes (#2002)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
petertonysmith94 authored Apr 4, 2024
1 parent 580eb37 commit 0be814e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 42 deletions.
7 changes: 7 additions & 0 deletions .changeset/slow-dancers-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@fuel-ts/abi-coder": minor
"@fuel-ts/contract": minor
"@fuel-ts/errors": minor
---

chore: remove redundant error codes
8 changes: 0 additions & 8 deletions packages/abi-coder/src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ export class Interface<TAbi extends JsonAbi = JsonAbi> {
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);
}

Expand All @@ -67,10 +63,6 @@ export class Interface<TAbi extends JsonAbi = JsonAbi> {
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);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/contract/src/contract-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
16 changes: 3 additions & 13 deletions packages/contract/src/util.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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}`);
25 changes: 13 additions & 12 deletions packages/errors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
```

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

Expand All @@ -105,7 +106,7 @@ function main() {
const prettyError = translateError(e);
console.log({ prettyError });
}
);
};
```

## Contributing
Expand Down
5 changes: 0 additions & 5 deletions packages/errors/src/error-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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
Expand Down Expand Up @@ -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',

Expand Down
4 changes: 2 additions & 2 deletions packages/errors/src/fuel-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 0be814e

Please sign in to comment.