Skip to content

Commit

Permalink
chore: switching Error to FuelError (#2879)
Browse files Browse the repository at this point in the history
* chore: generic error to fuel error

* chore: updated changeset

* chore: TOML spelling

* chore: fix lint error

---------

Co-authored-by: Miguel Oliveira <migueloliveiradev@gmail.com>
Co-authored-by: Nedim Salkić <nedim.salkic@fuel.sh>
  • Loading branch information
3 people authored Aug 5, 2024
1 parent 17130bd commit 751d638
Show file tree
Hide file tree
Showing 20 changed files with 228 additions and 112 deletions.
10 changes: 10 additions & 0 deletions .changeset/silly-kids-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@fuel-ts/account": patch
"@fuel-ts/contract": patch
"create-fuels": patch
"fuels": patch
"@fuel-ts/script": patch
"@fuel-ts/utils": patch
---

chore: switching `Error` to `FuelError`
24 changes: 24 additions & 0 deletions apps/docs/src/guide/errors/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ When an [`Account`](../../api/Account/Account.md) is required for an operation.

It could be caused during the deployments of contracts when an account is required to sign the transaction. This can be resolved by following the deployment guide [here](../contracts/deploying-contracts.md).

### `CONFIG_FILE_NOT_FOUND`

When a configuration file is not found. This could either be a `fuels.config.[ts,js,mjs,cjs]` file or a TOML file.

Ensure that the configuration file is present in the root directory of your project.

### `CONFIG_FILE_ALREADY_EXISTS`

When a configuration file already exists in the root directory of your project.

You can not run `fuels init` more than once for a given project. Either remove the existing configuration file or update it.

### `CONVERTING_FAILED`

When converting a big number into an incompatible format.
Expand Down Expand Up @@ -240,6 +252,12 @@ When the Fuel Node info cache is empty; This is usually caused by not being conn

Ensure that the provider has connected to a Fuel Node successfully.

### `TIMEOUT_EXCEEDED`

When the timeout has been exceeded for a given operation.

Check that you're connected to the network and that the network is stable.

### `TYPE_NOT_FOUND`

When the type with the given type ID is not found in the ABI.
Expand All @@ -264,6 +282,12 @@ A wallet manager will throw for a multitude of reasons. The error message will d

It could be that the passphrase is incorrect and/or the wallet does _not_ exist in the manager.

### `WORKSPACE_NOT_DETECTED`

When the workspace is not detected in the directory indicated in the message.

Ensure that the workspace is present in the directory specified.

### `HASHER_LOCKED`

The hashing algorithm is currently locked, any subsequent attempts to register a new implementation will throw this error.
Expand Down
41 changes: 21 additions & 20 deletions packages/account/src/connectors/fuel-connector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/require-await */
import { FuelError } from '@fuel-ts/errors';
import { EventEmitter } from 'events';

import type { TransactionRequestLike } from '../providers';
Expand Down Expand Up @@ -97,7 +98,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns Always true.
*/
async ping(): Promise<boolean> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -107,7 +108,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns boolean - connection status.
*/
async version(): Promise<Version> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -117,7 +118,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns The connection status.
*/
async isConnected(): Promise<boolean> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -127,7 +128,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns The accounts addresses strings
*/
async accounts(): Promise<Array<string>> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -140,7 +141,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns boolean - connection status.
*/
async connect(): Promise<boolean> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -151,7 +152,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns The connection status.
*/
async disconnect(): Promise<boolean> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -164,7 +165,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns Message signature
*/
async signMessage(_address: string, _message: string): Promise<string> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -177,7 +178,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns Transaction signature
*/
async signTransaction(_address: string, _transaction: TransactionRequestLike): Promise<string> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -194,7 +195,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns The transaction id
*/
async sendTransaction(_address: string, _transaction: TransactionRequestLike): Promise<string> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -206,7 +207,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns The current account selected otherwise null.
*/
async currentAccount(): Promise<string | null> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -221,7 +222,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns True if the asset was added successfully
*/
async addAssets(_assets: Array<Asset>): Promise<boolean> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -236,7 +237,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns True if the asset was added successfully
*/
async addAsset(_asset: Asset): Promise<boolean> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -245,7 +246,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns Array of assets metadata from the connector vinculated to the all accounts from a specific Wallet.
*/
async assets(): Promise<Array<Asset>> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -257,7 +258,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns Return true if the network was added successfully
*/
async addNetwork(_networkUrl: string): Promise<boolean> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -269,7 +270,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns Return true if the network was added successfully
*/
async selectNetwork(_network: Network): Promise<boolean> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -278,7 +279,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns Return all the networks added to the connector.
*/
async networks(): Promise<Array<Network>> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -287,7 +288,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns Return the current network selected inside the connector.
*/
async currentNetwork(): Promise<Network> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -298,7 +299,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns Return true if the ABI was added successfully.
*/
async addABI(_contractId: string, _abi: FuelABI): Promise<boolean> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -308,7 +309,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns The ABI if it exists, otherwise return null.
*/
async getABI(_id: string): Promise<FuelABI | null> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand All @@ -318,7 +319,7 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
* @returns Returns true if the abi exists or false if not.
*/
async hasABI(_id: string): Promise<boolean> {
throw new Error('Method not implemented.');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/account/src/connectors/fuel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ export class Fuel extends FuelConnector implements FuelSdk {
const hasConnector = await this.hasConnector();
await this.pingConnector();
if (!this._currentConnector || !hasConnector) {
throw new Error(
throw new FuelError(
ErrorCode.MISSING_CONNECTOR,
`No connector selected for calling ${method}. Use hasConnector before executing other methods.`
);
}
Expand Down Expand Up @@ -255,7 +256,7 @@ export class Fuel extends FuelConnector implements FuelSdk {
cacheTime: PING_CACHE_TIME,
})();
} catch {
throw new Error('Current connector is not available.');
throw new FuelError(ErrorCode.INVALID_PROVIDER, 'Current connector is not available.');
}
}

Expand Down
7 changes: 5 additions & 2 deletions packages/account/src/connectors/utils/promises.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { FuelError } from '@fuel-ts/errors';

// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable @typescript-eslint/no-explicit-any */
export type DeferPromise<R = unknown> = {
promise: Promise<R>;
resolve: (value: R) => void;
Expand All @@ -23,7 +26,7 @@ export async function withTimeout<F extends Promise<unknown>, RT = Awaited<F>>(
): Promise<RT> {
const timeoutPromise = new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('Promise timed out'));
reject(new FuelError(FuelError.CODES.TIMEOUT_EXCEEDED, 'Promise timed out'));
}, timeout);
});
return Promise.race([timeoutPromise, promise]) as any;
Expand Down
13 changes: 10 additions & 3 deletions packages/account/src/predicate/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,18 +228,25 @@ export class Predicate<TInputData extends InputValue[]> extends Account {

try {
if (!abiInterface) {
throw new Error(
throw new FuelError(
ErrorCode.INVALID_CONFIGURABLE_CONSTANTS,
'Cannot validate configurable constants because the Predicate was instantiated without a JSON ABI'
);
}

if (Object.keys(abiInterface.configurables).length === 0) {
throw new Error('Predicate has no configurable constants to be set');
throw new FuelError(
ErrorCode.INVALID_CONFIGURABLE_CONSTANTS,
'Predicate has no configurable constants to be set'
);
}

Object.entries(configurableConstants).forEach(([key, value]) => {
if (!abiInterface?.configurables[key]) {
throw new Error(`No configurable constant named '${key}' found in the Predicate`);
throw new FuelError(
ErrorCode.CONFIGURABLE_NOT_FOUND,
`No configurable constant named '${key}' found in the Predicate`
);
}

const { offset } = abiInterface.configurables[key];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { UTXO_ID_LEN } from '@fuel-ts/abi-coder';
import { Address, addressify } from '@fuel-ts/address';
import { ZeroBytes32 } from '@fuel-ts/address/configs';
import { randomBytes } from '@fuel-ts/crypto';
import { FuelError } from '@fuel-ts/errors';
import type { AddressLike, AbstractAddress, BytesLike } from '@fuel-ts/interfaces';
import type { BN, BigNumberish } from '@fuel-ts/math';
import { bn } from '@fuel-ts/math';
Expand Down Expand Up @@ -526,7 +527,7 @@ export abstract class BaseTransactionRequest implements BaseTransactionRequestLi
* @hidden
*/
metadataGas(_gasCosts: GasCosts): BN {
throw new Error('Not implemented');
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Not implemented');
}

/**
Expand Down
10 changes: 8 additions & 2 deletions packages/contract/src/contract-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,18 @@ export default class ContractFactory {
const hasConfigurable = Object.keys(this.interface.configurables).length;

if (!hasConfigurable) {
throw new Error('Contract does not have configurables to be set');
throw new FuelError(
ErrorCode.CONFIGURABLE_NOT_FOUND,
'Contract does not have configurables to be set'
);
}

Object.entries(configurableConstants).forEach(([key, value]) => {
if (!this.interface.configurables[key]) {
throw new Error(`Contract does not have a configurable named: '${key}'`);
throw new FuelError(
ErrorCode.CONFIGURABLE_NOT_FOUND,
`Contract does not have a configurable named: '${key}'`
);
}

const { offset } = this.interface.configurables[key];
Expand Down
11 changes: 9 additions & 2 deletions packages/create-fuels/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FuelError } from '@fuel-ts/errors';
import { versions } from '@fuel-ts/versions';
import toml from '@iarna/toml';
import chalk from 'chalk';
Expand Down Expand Up @@ -62,7 +63,10 @@ export const runScaffoldCli = async ({

// Exit the program if we are testing to prevent hanging
if (process.env.VITEST) {
throw new Error();
throw new FuelError(
FuelError.CODES.UNKNOWN,
'An error occurred due to the environmental variable `VITEST` was detected.'
);
}

projectPath = await promptForProjectPath();
Expand All @@ -73,7 +77,10 @@ export const runScaffoldCli = async ({

// Exit the program if we are testing to prevent hanging
if (process.env.VITEST) {
throw new Error();
throw new FuelError(
FuelError.CODES.UNKNOWN,
'An error occurred due to the environmental variable `VITEST` was detected.'
);
}

projectPath = await promptForProjectPath();
Expand Down
4 changes: 4 additions & 0 deletions packages/errors/src/error-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export enum ErrorCode {
INVALID_DATA = 'invalid-data',
FUNCTION_NOT_FOUND = 'function-not-found',
UNSUPPORTED_ENCODING_VERSION = 'unsupported-encoding-version',
TIMEOUT_EXCEEDED = 'timeout-exceeded',
CONFIG_FILE_NOT_FOUND = 'config-file-not-found',
CONFIG_FILE_ALREADY_EXISTS = 'config-file-already-exists',
WORKSPACE_NOT_DETECTED = 'workspace-not-detected',

// address
INVALID_BECH32_ADDRESS = 'invalid-bech32-address',
Expand Down
Loading

0 comments on commit 751d638

Please sign in to comment.