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: add UNKNOWN error code #2562

Merged
merged 22 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
350ac04
chore: added unknown error code
maschad Jun 19, 2024
06c1580
docs: add changesets
maschad Jun 20, 2024
f3cbdea
test: fix test for unknown error
maschad Jun 20, 2024
6213104
docs: update `UNKNOWN_ERROR` docs
maschad Jun 20, 2024
eae6742
docs: update `UNKNOWN_ERROR` message
maschad Jun 20, 2024
ffa6470
fix: update UNKNOWN error and return metadata
maschad Jun 21, 2024
d97f19e
Merge branch 'master' into mc/chore/unknown-error-code
maschad Jun 21, 2024
6a44dda
docs: update code to `UNKNOWN` in docs
maschad Jun 21, 2024
6c2b128
Merge branch 'master' into mc/chore/unknown-error-code
maschad Jun 21, 2024
836c004
fix: introduce property on FuelError class
maschad Jun 21, 2024
0591f1c
test: update test to include rawError
maschad Jun 21, 2024
88e08de
Merge branch 'master' into mc/chore/unknown-error-code
maschad Jun 23, 2024
8d3b062
Merge branch 'master' into mc/chore/unknown-error-code
petertonysmith94 Jun 24, 2024
6209bc1
Merge branch 'master' into mc/chore/unknown-error-code
maschad Jun 24, 2024
a50553e
chore: add equality check for raw error on fuel error
maschad Jun 24, 2024
b06c1c7
Merge branch 'master' into mc/chore/unknown-error-code
maschad Jun 24, 2024
882c70b
Merge branch 'master' into mc/chore/unknown-error-code
maschad Jun 24, 2024
6769e18
Merge branch 'master' into mc/chore/unknown-error-code
maschad Jun 24, 2024
21c7d2c
Merge branch 'master' into mc/chore/unknown-error-code
Torres-ssf Jun 24, 2024
6dae676
Merge branch 'master' into mc/chore/unknown-error-code
maschad Jun 24, 2024
522cb26
Merge branch 'master' into mc/chore/unknown-error-code
maschad Jun 24, 2024
62c6be8
Merge branch 'master' into mc/chore/unknown-error-code
maschad Jun 25, 2024
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
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,
arboleya marked this conversation as resolved.
Show resolved Hide resolved
`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;
maschad marked this conversation as resolved.
Show resolved Hide resolved
}

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 };
}
}
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
Loading