Skip to content

Add response in errors #46

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

Merged
merged 1 commit into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions xsuite/src/world/sworld.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,18 @@ test("SWallet.callContract with return", async () => {
assertHexList(txResult.returnData, ["01"]);
});

test("SWallet.callContract - Stack trace", async () => {
test("SWallet.callContract failure", async () => {
await expect(
wallet.callContract({
callee: contract,
funcName: "non_existent_function",
gasLimit: 10_000_000,
}),
).rejects.toMatchObject({
stack: expect.stringContaining("src/world/sworld.test.ts:"),
message: expect.stringMatching(
/^Transaction failed: 1 - invalid function \(not found\) - Response:\n{/,
),
stack: expect.stringMatching(/src\/world\/sworld\.test\.ts:[0-9]+:3\)$/),
});
});

Expand Down
44 changes: 27 additions & 17 deletions xsuite/src/world/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ export class World {
async #query(query: Query): Promise<QueryResult> {
const resQuery = await this.proxy.query(query);
if (![0, "ok"].includes(resQuery.returnCode)) {
throw new QueryError(resQuery.returnCode, resQuery.returnMessage);
throw new QueryError(
resQuery.returnCode,
resQuery.returnMessage,
resQuery,
);
}
return {
query: resQuery,
Expand Down Expand Up @@ -159,18 +163,18 @@ export class Wallet extends Signer {
const txHash = await this.proxy.sendTx(tx);
const resTx = await this.proxy.getCompletedTx(txHash);
if (resTx.status !== "success") {
throw new TxError("errorStatus", resTx.status);
throw new TxError("errorStatus", resTx.status, resTx);
}
if (resTx.executionReceipt?.returnCode) {
const { returnCode, returnMessage } = resTx.executionReceipt;
throw new TxError(returnCode, returnMessage);
throw new TxError(returnCode, returnMessage, resTx);
}
const signalErrorEvent = resTx?.logs?.events.find(
(e: any) => e.identifier === "signalError",
);
if (signalErrorEvent) {
const error = atob(signalErrorEvent.topics[1]);
throw new TxError("signalError", error);
throw new TxError("signalError", error, resTx);
}
return { tx: resTx };
}
Expand All @@ -188,13 +192,9 @@ export class Wallet extends Signer {
const txResult = await this.#executeTx(
Tx.getParamsToDeployContract(txParams),
);
const scDeployEvent = txResult.tx?.logs?.events.find(
const address = txResult.tx.logs.events.find(
(e: any) => e.identifier === "SCDeploy",
);
if (!scDeployEvent) {
throw new Error("No SCDeploy event.");
}
const address = scDeployEvent.address;
)!.address;
const contract = new Contract({ address, proxy: this.proxy });
const returnData = getTxReturnData(txResult.tx);
return { ...txResult, address, contract, returnData };
Expand Down Expand Up @@ -285,24 +285,34 @@ class InteractionError extends Error {
interaction: string;
code: number | string;
msg: string;
response: any;

constructor(interaction: string, code: number | string, msg: string) {
super(`${interaction} failed: ${code} - ${msg}`);
constructor(
interaction: string,
code: number | string,
message: string,
response: any,
) {
super(
`${interaction} failed: ${code} - ${message} - Response:\n` +
JSON.stringify(response, null, 2),
);
this.interaction = interaction;
this.code = code;
this.msg = msg;
this.msg = message;
this.response = response;
}
}

class TxError extends InteractionError {
constructor(code: number | string, message: string) {
super("Transaction", code, message);
constructor(code: number | string, message: string, response: any) {
super("Transaction", code, message, response);
}
}

class QueryError extends InteractionError {
constructor(code: number | string, message: string) {
super("Query", code, message);
constructor(code: number | string, message: string, response: any) {
super("Query", code, message, response);
}
}

Expand Down