diff --git a/.changeset/silly-kids-beg.md b/.changeset/silly-kids-beg.md new file mode 100644 index 0000000000..3b9f0ee32d --- /dev/null +++ b/.changeset/silly-kids-beg.md @@ -0,0 +1,8 @@ +--- +"@fuel-ts/account": patch +"@fuel-ts/contract": patch +"fuels": patch +"@fuel-ts/script": patch +--- + +chore: replacing all generic errors with `FuelError` diff --git a/packages/account/src/connectors/fuel.ts b/packages/account/src/connectors/fuel.ts index 415824edd4..4a5b23c04a 100644 --- a/packages/account/src/connectors/fuel.ts +++ b/packages/account/src/connectors/fuel.ts @@ -138,7 +138,8 @@ export class Fuel extends FuelConnector { 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.` ); } @@ -219,7 +220,7 @@ export class Fuel extends FuelConnector { cacheTime: PING_CACHE_TIME, })(); } catch { - throw new Error('Current connector is not available.'); + throw new FuelError(ErrorCode.INVALID_PROVIDER, 'Current connector is not available.'); } } diff --git a/packages/account/src/predicate/predicate.ts b/packages/account/src/predicate/predicate.ts index 8c5a390ae4..092f9d781e 100644 --- a/packages/account/src/predicate/predicate.ts +++ b/packages/account/src/predicate/predicate.ts @@ -228,18 +228,25 @@ export class Predicate 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]; diff --git a/packages/contract/src/contract-factory.ts b/packages/contract/src/contract-factory.ts index 76edd8b369..57d191bef1 100644 --- a/packages/contract/src/contract-factory.ts +++ b/packages/contract/src/contract-factory.ts @@ -177,12 +177,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]; diff --git a/packages/fuel-gauge/src/predicate/predicate-configurables.test.ts b/packages/fuel-gauge/src/predicate/predicate-configurables.test.ts index 9b20a5fb78..566c47f79b 100644 --- a/packages/fuel-gauge/src/predicate/predicate-configurables.test.ts +++ b/packages/fuel-gauge/src/predicate/predicate-configurables.test.ts @@ -1,6 +1,15 @@ import { generateTestWallet } from '@fuel-ts/account/test-utils'; +import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils'; import type { CoinQuantityLike } from 'fuels'; -import { getRandomB256, Provider, WalletUnlocked, Predicate, FUEL_NETWORK_URL } from 'fuels'; +import { + getRandomB256, + Provider, + WalletUnlocked, + Predicate, + FUEL_NETWORK_URL, + FuelError, + ErrorCode, +} from 'fuels'; import { FuelGaugeProjectsEnum, getFuelGaugeForcProject } from '../../test/fixtures'; @@ -187,52 +196,60 @@ describe('Predicate', () => { ).rejects.toThrow(/PredicateVerificationFailed/); }); - it('throws when setting configurable but predicate has none', () => { - expect(() => { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const predicate = new Predicate({ - bytecode: predicateBytesTrue, - abi: predicateAbiTrue, - provider: wallet.provider, - inputData: ['NADA'], - configurableConstants: { - constant: 'NADA', - }, - }); - }).toThrow('Predicate has no configurable constants to be set'); + it('throws when setting configurable but predicate has none', async () => { + await expectToThrowFuelError( + () => + new Predicate({ + bytecode: predicateBytesTrue, + abi: predicateAbiTrue, + provider: wallet.provider, + inputData: ['NADA'], + configurableConstants: { + constant: 'NADA', + }, + }), + new FuelError( + ErrorCode.INVALID_CONFIGURABLE_CONSTANTS, + 'Error setting configurable constants: Predicate has no configurable constants to be set.' + ) + ); }); - it('throws when setting invalid configurable', () => { - const errMsg = `Error setting configurable constants: No configurable constant named 'NOPE' found in the Predicate.`; - - expect(() => { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const predicate = new Predicate({ - bytecode: predicateBytesConfigurable, - abi: predicateAbiConfigurable, - provider: wallet.provider, - inputData: ['NADA'], - configurableConstants: { - NOPE: 'NADA', - }, - }); - }).toThrow(errMsg); + it('throws when setting invalid configurable', async () => { + await expectToThrowFuelError( + () => + new Predicate({ + bytecode: predicateBytesConfigurable, + abi: predicateAbiConfigurable, + provider: wallet.provider, + inputData: ['NADA'], + configurableConstants: { + NOPE: 'NADA', + }, + }), + new FuelError( + ErrorCode.INVALID_CONFIGURABLE_CONSTANTS, + `Error setting configurable constants: No configurable constant named 'NOPE' found in the Predicate.` + ) + ); }); - it('throws when setting a configurable with no ABI', () => { - const errMsg = `Error setting configurable constants: Cannot validate configurable constants because the Predicate was instantiated without a JSON ABI.`; - - expect(() => { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const predicate = new Predicate({ - bytecode: predicateBytesConfigurable, - provider: wallet.provider, - inputData: ['NADA'], - configurableConstants: { - NOPE: 'NADA', - }, - }); - }).toThrow(errMsg); + it('throws when setting a configurable with no ABI', async () => { + await expectToThrowFuelError( + () => + new Predicate({ + bytecode: predicateBytesConfigurable, + provider: wallet.provider, + inputData: ['NADA'], + configurableConstants: { + NOPE: 'NADA', + }, + }), + new FuelError( + ErrorCode.INVALID_CONFIGURABLE_CONSTANTS, + `Error setting configurable constants: Cannot validate configurable constants because the Predicate was instantiated without a JSON ABI.` + ) + ); }); }); }); diff --git a/packages/fuels/src/cli/commands/deploy/createWallet.ts b/packages/fuels/src/cli/commands/deploy/createWallet.ts index 112fea6a17..3d8bd232ab 100644 --- a/packages/fuels/src/cli/commands/deploy/createWallet.ts +++ b/packages/fuels/src/cli/commands/deploy/createWallet.ts @@ -1,5 +1,5 @@ import { Wallet, Provider } from '@fuel-ts/account'; -import { FuelError } from '@fuel-ts/errors'; +import { ErrorCode, FuelError } from '@fuel-ts/errors'; export async function createWallet(providerUrl: string, privateKey?: string) { let pvtKey: string; @@ -9,7 +9,10 @@ export async function createWallet(providerUrl: string, privateKey?: string) { } else if (process.env.PRIVATE_KEY) { pvtKey = process.env.PRIVATE_KEY; } else { - throw new Error('You must provide a privateKey via config.privateKey or env PRIVATE_KEY'); + throw new FuelError( + ErrorCode.MISSING_REQUIRED_PARAMETER, + 'You must provide a privateKey via config.privateKey or env PRIVATE_KEY' + ); } try { @@ -20,7 +23,7 @@ export async function createWallet(providerUrl: string, privateKey?: string) { const error = e as Error & { cause?: { code: string } }; if (/EADDRNOTAVAIL|ECONNREFUSED/.test(error.cause?.code ?? '')) { throw new FuelError( - FuelError.CODES.CONNECTION_REFUSED, + ErrorCode.CONNECTION_REFUSED, `Couldn't connect to the node at "${providerUrl}". Check that you've got a node running at the config's providerUrl or set autoStartFuelCore to true.` ); } else { diff --git a/packages/script/src/script.test.ts b/packages/script/src/script.test.ts index f11dfa59bb..f160e3bef1 100644 --- a/packages/script/src/script.test.ts +++ b/packages/script/src/script.test.ts @@ -5,7 +5,8 @@ import type { Account, TransactionResponse, TransactionResult } from '@fuel-ts/a import { Provider, ScriptTransactionRequest } from '@fuel-ts/account'; import { FUEL_NETWORK_URL } from '@fuel-ts/account/configs'; import { generateTestWallet } from '@fuel-ts/account/test-utils'; -import { safeExec } from '@fuel-ts/errors/test-utils'; +import { ErrorCode, FuelError } from '@fuel-ts/errors'; +import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils'; import type { BigNumberish } from '@fuel-ts/math'; import { bn } from '@fuel-ts/math'; import { ScriptRequest } from '@fuel-ts/program'; @@ -129,11 +130,13 @@ describe('Script', () => { const newScript = new Script(scriptBin, jsonAbiFragmentMock, wallet); - const { error } = await safeExec(() => newScript.setConfigurableConstants({ FEE: 8 })); - - const errMsg = `Error setting configurable constants: The script does not have configurable constants to be set.`; - - expect((error).message).toBe(errMsg); + await expectToThrowFuelError( + () => newScript.setConfigurableConstants({ FEE: 8 }), + new FuelError( + ErrorCode.INVALID_CONFIGURABLE_CONSTANTS, + 'Error setting configurable constants: The script does not have configurable constants to be set.' + ) + ); }); it('should throw when setting configurable with wrong name', async () => { @@ -156,10 +159,12 @@ describe('Script', () => { const script = new Script(scriptBin, jsonAbiWithConfigurablesMock, wallet); - const { error } = await safeExec(() => script.setConfigurableConstants({ NOT_DEFINED: 8 })); - - const errMsg = `Error setting configurable constants: The script does not have a configurable constant named: 'NOT_DEFINED'.`; - - expect((error).message).toBe(errMsg); + await expectToThrowFuelError( + () => script.setConfigurableConstants({ NOT_DEFINED: 8 }), + new FuelError( + ErrorCode.INVALID_CONFIGURABLE_CONSTANTS, + `Error setting configurable constants: The script does not have a configurable constant named: 'NOT_DEFINED'.` + ) + ); }); }); diff --git a/packages/script/src/script.ts b/packages/script/src/script.ts index a9daae7994..6352738edb 100644 --- a/packages/script/src/script.ts +++ b/packages/script/src/script.ts @@ -91,12 +91,18 @@ export class Script, TOutput> extends AbstractScript { setConfigurableConstants(configurables: { [name: string]: unknown }) { try { if (!Object.keys(this.interface.configurables).length) { - throw new Error(`The script does not have configurable constants to be set`); + throw new FuelError( + ErrorCode.INVALID_CONFIGURABLE_CONSTANTS, + `The script does not have configurable constants to be set` + ); } Object.entries(configurables).forEach(([key, value]) => { if (!this.interface.configurables[key]) { - throw new Error(`The script does not have a configurable constant named: '${key}'`); + throw new FuelError( + ErrorCode.CONFIGURABLE_NOT_FOUND, + `The script does not have a configurable constant named: '${key}'` + ); } const { offset } = this.interface.configurables[key];