Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: replacing all generic errors with FuelError #2549

Closed
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/silly-kids-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@fuel-ts/account": patch
"@fuel-ts/contract": patch
"fuels": patch
"@fuel-ts/script": patch
---

chore: switching `Error` to `FuelError`
mvares marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 3 additions & 2 deletions packages/account/src/connectors/fuel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`
);
}
Expand Down Expand Up @@ -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.');
}
}

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
10 changes: 8 additions & 2 deletions packages/contract/src/contract-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
103 changes: 60 additions & 43 deletions packages/fuel-gauge/src/predicate/predicate-configurables.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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',
},
}),
{
code: ErrorCode.INVALID_CONFIGURABLE_CONSTANTS,
message: expect.stringContaining('Predicate has no configurable constants to be set'),
}
Copy link
Contributor

@nedsalk nedsalk Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msensys the expectToThrowFuelError utility internally checks for message (and any other property) equality. You shouldn't be passing this as its second argument:

{
  code: ErrorCode.INVALID_CONFIGURABLE_CONSTANTS,
  message: expect.stringContaining('Predicate has no configurable constants to be set'),
}

but rather this:

new FuelError(
  ErrorCode.INVALID_CONFIGURABLE_CONSTANTS,
  'Predicate has no configurable constants to be set'
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why, but the test isn't passing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nedsalk look at here:

- [FuelError: Predicate has no configurable constants to be set]
+ Object {
+   "VERSIONS": Object {
+     "FORC": "0.60.0",
+     "FUELS": "0.90.0",
+     "FUEL_CORE": "0.30.0",
+   },
+   "code": "invalid-configurable-constants",
+   "metadata": Object {},
+   "name": "FuelError",
+ }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any failing tests; CI is green for all workflows.

But as @nedsalk said, you should not assert the error with an Object:

{
code: ErrorCode.INVALID_CONFIGURABLE_CONSTANTS,
message: expect.stringContaining('Predicate has no configurable constants to be set'),
}

Instead, you should use FuelError, as you did on the other occurrences:

new FuelError(
ErrorCode.INVALID_CONFIGURABLE_CONSTANTS,
`Error setting configurable constants: No configurable constant named 'NOPE' found in the Predicate.`
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I committed the change you mentioned. Take a look at CI

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msensys the test is failing because of this line catching and rethrowing the error with an updated message. To fix the failing test, change the expected message in your test to Error setting configurable constants: Predicate has no configurable constants to be set.; you were missing the Error setting configurable constants: part. We should maybe rethink this try/catch here, but it's out of the scope of this issue. Just changing the expected message is enough.

);
});

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.`
)
);
});
});
});
9 changes: 6 additions & 3 deletions packages/fuels/src/cli/commands/deploy/createWallet.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
27 changes: 16 additions & 11 deletions packages/script/src/script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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>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 () => {
Expand All @@ -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>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'.`
)
);
});
});
10 changes: 8 additions & 2 deletions packages/script/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,18 @@ export class Script<TInput extends Array<any>, 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];
Expand Down
Loading