Skip to content

Commit

Permalink
chore: add UNKNOWN error code (#2562)
Browse files Browse the repository at this point in the history
Co-authored-by: Anderson Arboleya <anderson@arboleya.me>
Co-authored-by: Peter Smith <peter@blueoceancomputing.co.uk>
Co-authored-by: Sérgio Torres <30977845+Torres-ssf@users.noreply.github.com>
  • Loading branch information
4 people authored Jun 25, 2024
1 parent 3dee29a commit eec0806
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 30 deletions.
6 changes: 6 additions & 0 deletions .changeset/stale-starfishes-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fuel-ts/account": patch
"@fuel-ts/errors": patch
---

chore: add `UNKNOWN` error code
6 changes: 6 additions & 0 deletions apps/docs/src/guide/errors/error-codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,9 @@ It could be that the passphrase is incorrect and/or the wallet does _not_ exist

The hashing algorithm is currently locked, any subsequent attempts to register a new implementation will throw this error.
The purpose of the lock function is to provide a way to ensure that the implementation of the specific hashing algorithm cannot be changed once it is locked. This can be useful in scenarios where you want to guarantee the integrity and consistency of the hashing function throughout your application.

## `UNKNOWN`

In cases where the error hasn't been mapped yet, this code will be used.

If you believe you found a bug, please report the [issue](https://github.com/FuelLabs/fuels-ts/issues/new/choose) to the team.
42 changes: 28 additions & 14 deletions packages/account/src/providers/utils/extract-tx-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ import type { TransactionResultReceipt } from '../transaction-response';
* @param status - The transaction failure status.
* @returns The error message.
*/
export const assemblePanicError = (statusReason: string) => {
export const assemblePanicError = (
statusReason: string,
metadata: Record<string, unknown>
): FuelError => {
let errorMessage = `The transaction reverted with reason: "${statusReason}".`;

if (PANIC_REASONS.includes(statusReason)) {
errorMessage = `${errorMessage}\n\nYou can read more about this error at:\n\n${PANIC_DOC_URL}#variant.${statusReason}`;
}

return { errorMessage, reason: statusReason };
return new FuelError(ErrorCode.SCRIPT_REVERTED, errorMessage, {
...metadata,
reason: statusReason,
});
};

/** @hidden */
Expand All @@ -40,8 +46,9 @@ const stringify = (obj: unknown) => JSON.stringify(obj, null, 2);
*/
export const assembleRevertError = (
receipts: Array<TransactionResultReceipt>,
logs: Array<unknown>
) => {
logs: Array<unknown>,
metadata: Record<string, unknown>
): FuelError => {
let errorMessage = 'The transaction reverted with an unknown reason.';

const revertReceipt = receipts.find(({ type }) => type === ReceiptType.Revert) as ReceiptRevert;
Expand Down Expand Up @@ -88,12 +95,21 @@ export const assembleRevertError = (
break;

default:
reason = 'unknown';
errorMessage = `The transaction reverted with an unknown reason: ${revertReceipt.val}`;
throw new FuelError(
ErrorCode.UNKNOWN,
`The transaction reverted with an unknown reason: ${revertReceipt.val}`,
{
...metadata,
reason: 'unknown',
}
);
}
}

return { errorMessage, reason };
return new FuelError(ErrorCode.SCRIPT_REVERTED, errorMessage, {
...metadata,
reason,
});
};

interface IExtractTxError {
Expand All @@ -112,18 +128,16 @@ export const extractTxError = (params: IExtractTxError): FuelError => {

const isPanic = receipts.some(({ type }) => type === ReceiptType.Panic);
const isRevert = receipts.some(({ type }) => type === ReceiptType.Revert);

const { errorMessage, reason } = isPanic
? assemblePanicError(statusReason)
: assembleRevertError(receipts, logs);

const metadata = {
logs,
receipts,
panic: isPanic,
revert: isRevert,
reason,
reason: '',
};

return new FuelError(ErrorCode.SCRIPT_REVERTED, errorMessage, metadata);
if (isPanic) {
return assemblePanicError(statusReason, metadata);
}
return assembleRevertError(receipts, logs, metadata);
};
3 changes: 3 additions & 0 deletions packages/errors/src/error-codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,7 @@ export enum ErrorCode {

// graphql
STREAM_PARSING_ERROR = 'stream-parsing-error',

// Unknown
UNKNOWN = 'unknown',
}
9 changes: 8 additions & 1 deletion packages/errors/src/fuel-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,12 @@ it('converts error to plain object', () => {
const message = 'It happens';
const metadata = { name: 'FuelLabs' };
const err = new FuelError(code, message, metadata);
expect(err.toObject()).toEqual({ code, name, message, VERSIONS: err.VERSIONS, metadata });
expect(err.toObject()).toEqual({
code,
name,
message,
VERSIONS: err.VERSIONS,
metadata,
rawError: {},
});
});
13 changes: 10 additions & 3 deletions packages/errors/src/fuel-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class FuelError extends Error {
static readonly CODES = ErrorCode;
readonly VERSIONS = versions;
readonly metadata: Record<string, unknown>;
readonly rawError: unknown;

static parse(e: unknown) {
const error = e as FuelError;
Expand All @@ -32,15 +33,21 @@ export class FuelError extends Error {

code: ErrorCode;

constructor(code: ErrorCode, message: string, metadata: Record<string, unknown> = {}) {
constructor(
code: ErrorCode,
message: string,
metadata: Record<string, unknown> = {},
rawError: unknown = {}
) {
super(message);
this.code = code;
this.name = 'FuelError';
this.metadata = metadata;
this.rawError = rawError;
}

toObject() {
const { code, name, message, metadata, VERSIONS } = this;
return { code, name, message, metadata, VERSIONS };
const { code, name, message, metadata, VERSIONS, rawError } = this;
return { code, name, message, metadata, VERSIONS, rawError };
}
}
8 changes: 8 additions & 0 deletions packages/errors/src/test-utils/expect-to-throw-fuel-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ export const expectToThrowFuelError = async (
expect(thrownError.metadata).toEqual(expect.objectContaining(expectedError.metadata));
}

if (expectedError.message) {
expect(thrownError.message).toEqual(expectedError.message);
}

if (expectedError.rawError) {
expect(thrownError.rawError).toEqual(expectedError.rawError);
}

expect(thrownError.name).toEqual('FuelError');
expect(thrownError).toMatchObject(expectedError);
};
20 changes: 8 additions & 12 deletions packages/fuel-gauge/src/revert-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,16 @@ describe('Revert Error Testing', () => {
);
});

it('should throw for explicit "revert" call', async () => {
it('should throw UNKNOWN Error for revert', async () => {
await expectToThrowFuelError(
() => contractInstance.functions.revert_with_0().call(),
new FuelError(
ErrorCode.SCRIPT_REVERTED,
`The transaction reverted with an unknown reason: 0`,
{
logs: [],
receipts: expect.any(Array<TransactionResultReceipt>),
panic: false,
revert: true,
reason: 'unknown',
}
)
new FuelError(ErrorCode.UNKNOWN, `The transaction reverted with an unknown reason: 0`, {
logs: [],
receipts: expect.any(Array<TransactionResultReceipt>),
panic: false,
revert: true,
reason: 'unknown',
})
);
});

Expand Down

0 comments on commit eec0806

Please sign in to comment.