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 4 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
4 changes: 4 additions & 0 deletions .changeset/silly-kids-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---
mvares marked this conversation as resolved.
Show resolved Hide resolved

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.JSON_ABI_ERROR,
'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
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
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