Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
nedsalk committed Jul 1, 2024
1 parent f4f98ef commit e31fb99
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 11 deletions.
44 changes: 36 additions & 8 deletions packages/account/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { arrayify, hexlify, DateTime } from '@fuel-ts/utils';
import { checkFuelCoreVersionCompatibility } from '@fuel-ts/versions';
import { equalBytes } from '@noble/curves/abstract/utils';
import type { DocumentNode } from 'graphql';
import type { DocumentNode, GraphQLError } from 'graphql';
import { GraphQLClient } from 'graphql-request';
import type { GraphQLResponse } from 'graphql-request/src/types';
import { clone } from 'ramda';
Expand Down Expand Up @@ -404,7 +404,30 @@ export default class Provider {
fullRequest = await options.requestMiddleware(fullRequest);
}

return options.fetch ? options.fetch(url, fullRequest, options) : fetch(url, fullRequest);
const response = await (options.fetch
? options.fetch(url, fullRequest, options)
: fetch(url, fullRequest));

const body = await response.clone().json();

if (Array.isArray(body.errors)) {
console.log('got the resposne');

Check warning on line 414 in packages/account/src/providers/provider.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement

const error: GraphQLError = body.errors[0];
let fuelError: FuelError = new FuelError(ErrorCode.UNKNOWN, '', undefined, error);
if (error) {
switch (error.message) {
case 'not enough coins to fit the target':
fuelError = new FuelError(ErrorCode.NOT_ENOUGH_COINS, '');
break;
default:
break;
}
}
throw fuelError;
}

return response;
}, retryOptions);
}

Expand Down Expand Up @@ -552,12 +575,17 @@ Supported fuel-core version: ${supportedVersion}.`
responseMiddleware: (response: GraphQLResponse<unknown> | Error) => {
if ('response' in response) {
const graphQlResponse = response.response as GraphQLResponse;
if (Array.isArray(graphQlResponse?.errors)) {
throw new FuelError(
FuelError.CODES.INVALID_REQUEST,
graphQlResponse.errors.map((err: Error) => err.message).join('\n\n')
);
}
const error = graphQlResponse.errors?.[0];

Check warning on line 578 in packages/account/src/providers/provider.ts

View workflow job for this annotation

GitHub Actions / Lint

'error' is assigned a value but never used

// if (Array.isArray(graphQlResponse?.errors)) {
// console.log(graphQlResponse.errors);
// throw new FuelError(
// FuelError.CODES.INVALID_REQUEST,
// graphQlResponse.errors.map((err: Error) => err.message).join('\n\n'),
// undefined,
// graphQlResponse.errors[0]
// );
// }
}
},
});
Expand Down
3 changes: 3 additions & 0 deletions packages/errors/src/error-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ export enum ErrorCode {
// graphql
STREAM_PARSING_ERROR = 'stream-parsing-error',

// VM Errors
NOT_ENOUGH_COINS = 'not-enough-coins',

// Unknown
UNKNOWN = 'unknown',
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Contract, Wallet } from 'fuels';
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';
import { Contract, ErrorCode, FuelError, Wallet } from 'fuels';
import { launchTestNode } from 'fuels/test-utils';

import {
Expand Down Expand Up @@ -64,8 +65,13 @@ describe('Predicate', () => {

// calling the contract with the receiver account (no resources)
contract.account = receiver;
await expect(contract.functions.mint_coins(200).call()).rejects.toThrow(
/not enough coins to fit the target/

await contract.functions.mint_coins(200).call();
await expectToThrowFuelError(
() => contract.functions.mint_coins(200).call(),
new FuelError(ErrorCode.NOT_ENOUGH_COINS, '', undefined, {
message: 'not enough coins to fit the target',
})
);

// setup predicate
Expand Down
25 changes: 25 additions & 0 deletions packages/fuel-gauge/src/vm-error-mappings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expectToThrowFuelError, safeExec } from '@fuel-ts/errors/test-utils';

Check warning on line 1 in packages/fuel-gauge/src/vm-error-mappings.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'expectToThrowFuelError' is defined but never used

Check warning on line 1 in packages/fuel-gauge/src/vm-error-mappings.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'safeExec' is defined but never used
import { ErrorCode, FuelError, WalletUnlocked } from 'fuels';

Check warning on line 2 in packages/fuel-gauge/src/vm-error-mappings.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'ErrorCode' is defined but never used

Check warning on line 2 in packages/fuel-gauge/src/vm-error-mappings.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'FuelError' is defined but never used
import { launchTestNode } from 'fuels/test-utils';

describe('vm error mappings', () => {
test.each([false])(
'not enough coins to fit the target (subscription: %s)',
async (awaitExecution) => {
using launched = await launchTestNode();

const { provider } = launched;

const sender = WalletUnlocked.generate({ provider });
const receiver = WalletUnlocked.generate();
const tx = await sender.createTransfer(receiver.address, 10);

await provider.sendTransaction(tx, { awaitExecution });

// await expectToThrowFuelError(
// () => provider.sendTransaction(tx, { awaitExecution }),
// new FuelError(ErrorCode.NOT_ENOUGH_COINS, '')
// );
}
);
});

0 comments on commit e31fb99

Please sign in to comment.