coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.36k stars 1.25k forks source link

Improve error handling by adding transaction/simulation logs #2980

Closed Woody4618 closed 3 weeks ago

Woody4618 commented 1 month ago

I would love to improve error messages. Especially when running tests.

At the moment what you see when there is an error in a test is only last line of the transaction logs or the status returned in SendAndConfirmTransaction from web3js like so:

image

And this with preflight checks disabled:

image

So at the moment the only way to see the actual error is to disabled preflight checks and look at the on chain error.

What I would love to have instead is printing out the whole logs like so:

image

I think it would be possible to add this logic into web3js in the error case of sendAndConfirmTransaction here: https://github.com/solana-labs/solana-web3.js/blob/45216e2afb6940d1055976431e55024d235d9b56/packages/library-legacy/src/utils/send-and-confirm-transaction.ts#L18

Looks like anchor is using an old version of web3js 1.68. Is there a reason for that or the plan to update it? Do you think this is worth proceeding? I think it would greatly benefit developer experience. Or do you maybe have a better idea on how to achieve better errors and/or adding the transaction/simulation logs?

acheroncrypto commented 1 month ago

I would love to improve error messages. Especially when running tests.

Same. We can make debugging errors much smoother.

Looks like anchor is using an old version of web3js 1.68. Is there a reason for that or the plan to update it? Do you think this is worth proceeding? I think it would greatly benefit developer experience. Or do you maybe have a better idea on how to achieve better errors and/or adding the transaction/simulation logs?

That's not correct.

https://github.com/coral-xyz/anchor/blob/334a44ab7b2ea951af8001343e05244520d4e71c/ts/packages/anchor/package.json#L38

^1.68.0 version requirement means anything above that version will work, and when you initialize a new project or run yarn upgrade, it will get the latest compatible version (of SemVer) by default which will be version 1.91.8 as of writing this comment. You can easily confirm this with anchor init check-web3js-version and checking the entry for the @solana/web3.js package in yarn.lock.

Woody4618 commented 1 month ago

Awesome, thank you. I will start working on this.

Woody4618 commented 1 month ago

I created this PR to add logs to sendAndConfirm and sendEncodedTransaction. https://github.com/solana-labs/solana-web3.js/pull/2736

I am having troubles testing it in my anchor project though.

If I directly use the function from web3Js sendAndConfirmTransaction it uses my linked package that I linked via pnpm link.

This is my expected outcome which i want to achieve.

  0 passing (1s)
  1 failing

  1) anchor_error_test
       Is initialized!:
     Error: Transaction 28ZnX5TMNaMHE9L13bjdDwbeyABoWvACVwah8MKd98rxPuqWifujq5bx3YeAdTjwHJwmEHcJiYPGB9k6SHDobLmt failed: [
  "Program 11111111111111111111111111111111 invoke [1]",
  "Transfer: insufficient lamports 1000000000, need 2000000000",
  "Program 11111111111111111111111111111111 failed: custom program error: 0x1"
]
      at sendAndConfirmTransaction (/Users/jonasmac2/Documents/GitHub/solana-web3.js/packages/library-legacy/src/utils/send-and-confirm-transaction.ts:95:15)
      at processTicksAndRejections (node:internal/process/task_queues:95:5)

But if I use the .rpc() function like this:

    const tx = await program.methods.initialize().rpc({ skipPreflight: true });
    console.log("Your transaction signature", tx);

the .rpc() function seems to use some other location for connection and it does not use my linked package.

  1) anchor_error_test
       Is initialized!:
     Error: Raw transaction 4Eec1YXVqaFzGAj45sJYfgMMZFV63PahzEFBPkqeLRd3ahCxkTvNfBWcjMEL5onHry3j629tnSBwYtyj4PsQEj2u failed ({"err":{"InstructionError":[0,"ProgramFailedToComplete"]}})
      at sendAndConfirmRawTransaction (node_modules/.pnpm/@coral-xyz+anchor@0.30.0_bufferutil@4.0.8_utf-8-validate@5.0.10/node_modules/@coral-xyz/anchor/src/provider.ts:386:11)
      at processTicksAndRejections (node:internal/process/task_queues:95:5)
      at AnchorProvider.sendAndConfirm (node_modules/.pnpm/@coral-xyz+anchor@0.30.0_bufferutil@4.0.8_utf-8-validate@5.0.10/node_modules/@coral-xyz/anchor/src/provider.ts:163:14)
      at MethodsBuilder.rpc [as _rpcFn] (node_modules/.pnpm/@coral-xyz+anchor@0.30.0_bufferutil@4.0.8_utf-8-validate@5.0.10/node_modules/@coral-xyz/anchor/src/program/namespace/rpc.ts:29:16)

How can I locally test if my web3js changes also work for anchor?

Woody4618 commented 3 weeks ago

Solved this in web3js with this pr by adding error logs to sendTransactionError: https://github.com/solana-labs/solana-web3.js/pull/2736

The error during tests look like this now:

image