Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

[experimental] Convert all thrown errors to SolanaError #2118

Closed
steveluscher opened this issue Feb 15, 2024 · 8 comments · Fixed by #2182, #2184, #2185, #2186 or #2187
Closed

[experimental] Convert all thrown errors to SolanaError #2118

steveluscher opened this issue Feb 15, 2024 · 8 comments · Fixed by #2182, #2184, #2185, #2186 or #2187
Assignees
Labels
enhancement New feature or request released

Comments

@steveluscher
Copy link
Contributor

  1. Search everywhere except for packages/library-legacy for throw new Error
  2. Convert the error to a coded SolanaError
    a. Create a code for it in packages/errors/src/codes.ts
    b. Optionally allow the caller to add typed context to the error by defining the type in packages/errors/src/context.ts
    c. Create a message for it in packages/errors/src/messages.ts
    d. Replace the callsite with throw new SolanaError(SOLANA_ERROR__MY_NEW_ERROR, {/* optional context */})
  3. Make sure that the generated Kinobi clients throw ProgramErrors from on-chain programs in the same way (include the logs, message, program address, error code, etc in the error's context)
@buffalojoec
Copy link
Contributor

I want you guys to know I cried when I self-assigned this.

@lorisleiva
Copy link
Contributor

Thank you @buffalojoec 🫡

I can tackle the errors inside the @solana/programs package since the error resolution will have to change anyway such that we don't need to provide the array of programs as we do now.

@lorisleiva
Copy link
Contributor

@buffalojoec Actually you might also want to avoid tackling all RPC errors right now as I'm working on a massive refactoring stack.

@buffalojoec
Copy link
Contributor

@buffalojoec Actually you might also want to avoid tackling all RPC errors right now as I'm working on a massive refactoring stack.

You got it, just throw a flag here whenever I'm good to go.

@steveluscher
Copy link
Contributor Author

We can split this up if you want! Want to work from A-Z and I'll work from Z-A @buffalojoec?

@buffalojoec
Copy link
Contributor

We can split this up if you want! Want to work from A-Z and I'll work from Z-A @buffalojoec?

Sure! Are you just basing off of Loris's stack?

steveluscher added a commit that referenced this issue Feb 29, 2024
)

# Summary

Wouldn't it be nice if you could `catch` _particular_ transaction errors in your application, like `BlockhashNotFound`, and choose the correct mitigation based on the type of transaction error?

In this PR, we introduce coded exceptions for each `TransactionError` returned from the RPC's `sendTransaction` method.

> [!NOTE]
> Because the RPC doesn't return structured errors or error codes, we had to break our own rules in this PR and hardcode a map between the error names and the code numbers. My [first crack](https://gist.github.com/steveluscher/aaa7cbbb5433b1197983908a40860c47#file-fml-ts-L12) at this employed a source code compression scheme that I later deemed too risky for the 250 gzipped bytes it saved. We might consider such a scheme in the future, especially since the next PR will add `InstructionError` to the mix.

# Test Plan

```shell
cd packages/errors
pnpm test:unit:browser
pnpm test:unit:node
```

Addresses #2118.
steveluscher added a commit that referenced this issue Feb 29, 2024
# Summary

In a future PR when we add `InstructionError` the core mucking code is going to look exactly the same as this. Let's extract it to share with that function, down the line.

Addresses #2118.
steveluscher added a commit that referenced this issue Feb 29, 2024
# Summary

In this PR, we build atop the work in #2213 and introduce coded exceptions for each `InstructionError` returned from the RPC's `sendTransaction` method.

> [!NOTE]
> Because the RPC doesn't return structured errors or error codes, we had to break our own rules in this PR and hardcode a map between the error names and the code numbers. My [first crack](https://gist.github.com/steveluscher/aaa7cbbb5433b1197983908a40860c47#file-fml-ts-L12) at this employed a source code compression scheme that I later deemed too risky for the 250 gzipped bytes it saved. We might consider such a scheme in the future, especially since the next PR will add `InstructionError` to the mix.

# Test Plan

```shell
cd packages/errors
pnpm test:unit:browser
pnpm test:unit:node
```

Addresses #2118.
@steveluscher steveluscher reopened this Feb 29, 2024
steveluscher added a commit that referenced this issue Mar 1, 2024
#2187 buried these address sub errors in such a way that they'd never reach the caller. This PR allows them to bubble up.

Addresses #2118.
steveluscher added a commit that referenced this issue Mar 4, 2024
# Summary

Invariant violations are a kind of error that should never get hit ever, and if they do your program is broken.

In one sense we don't want error codes for them because they're not intended to be caught in downstream programs. On the other hand I've tried to come up with a reason _not_ to give them codes and I can't.

Making them a `@solana/error` gives us compression and error decoding for free.

Addresses #2118.
steveluscher pushed a commit that referenced this issue Mar 5, 2024
…2266)

Re-assign error codes such that they start from `xxxx000` within each group and from `1` for ungrouped errors.

Closes #2118.
Copy link
Contributor

github-actions bot commented Mar 7, 2024

🎉 This issue has been resolved in version 1.91.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

steveluscher added a commit that referenced this issue Mar 12, 2024
# Summary

So wow. I completely missed these as part of #2118, and it turns out they're a really big deal and required a ton of changes.

There are:

* Errors that have enough data to format a message
* Errors that have no data, but also no context in the message
* Errors that have no data, but really should, because you can't format a message without it
* Preflight errors in which is nested a `TransactionError` (see #2213)

In this PR we create a helper that takes in the `RpcSimulateTransactionResult` from the RPC and reformats it as a coded `SolanaError`.

As always, everything you need to know is in the `packages/errors/src/__tests__/json-rpc-error-test.ts`.

# Test Plan

```
pnpm turbo test:unit:browser
pnpm turbo test:unit:node
```
Copy link
Contributor

Because there has been no activity on this issue for 7 days since it was closed, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.