-
Notifications
You must be signed in to change notification settings - Fork 928
A better way to resolve custom error messages #2019
Comments
Hey Mike, we're working on it for the new Web3.js. Have you seen this? [Copy/pasting the relevant section below]. These create program functions will be generated for each program and will allow us to transform an hex code into an actual program error.
|
Great minds etc! 😃 Thanks @lorisleiva ! Would it be possible to allow |
The That source of information here is being explicitly requested by the function as otherwise we would need a centralised registry of program error codes to fetch from. Is that what you are referring to? P.S.: You might be interested in point [E] of the thread. |
Exactly! Ie if the IDL is published, we fetch the errors from the IDL. If the IDL is not published, we tell this explicitly to the user and return Downsides: we have an HTTP request to resolve the error. Users could turn off custom errors handling if they wanted to to disable this behavior though. Upsides: we have useful errors by default and don't ask users to provide/maintain a list of all the programs they want to use, or have something that recursively gets the programs another program may CPI to per [E] |
Gotcha! I think having an additional asynchronous helper method that uses the Anchor IDL registry for that purpose makes total sense. However, I do think we should keep the synchronous method for situations where we just want to use the information provided by the generated clients to avoid an extra HTTP call that may not even resolve. I'd also like to explore a plugin ecosystem on top of the web3.js library that would help bind all the components together. For instance, this would be much easier to handle with a program repository plugin (which is how Umi handles this problem). |
would it be possible to bubble custom it is less than ideal to have to parse message strings everywhere when implementing error handling. some of them are rpc/sending related, some might be network, some may or may not be program specific. for eg. the custom error class |
We now have first-class try {
// Something.
} catch (e) {
if (isSolanaError(e, SOLANA_ERROR__JSON_RPC__SERVER_ERROR_SEND_TRANSACTION_PREFLIGHT_FAILURE)) {
// Now TypeScript knows that you have e.context.logs and e.context.returnData and stuff.
// But also…
if (isSolanaError(e.cause, SOLANA_ERROR__INSTRUCTION_ERROR__CUSTOM)) {
// Now typescript has e.cause.context.code and e.cause.context.index
}
}
} |
@steveluscher When is it going to be released? I see it's in the technical preview version, but not the stable one. I'd love to use the TP in my project, but I've only found one demo and there's no other examples or documentation. Am I missing something? |
@mlshv Have you checked out this README in the main library? It's a little hidden, but we've kept that up to date. Besides that, some of the packages' READMEs are detailed, and some aren't. Admittedly we don't have end-to-end docs like we'd want to have, but that's mainly because everything has been changing so much. Perhaps Stack Exchange can be a decent medium until then? Also, we have published a "Technology Preview 2", which contains the custom errors, as well as the errors package itself. |
Errors are so much better now in the 2.0 line of web3.js, and the addition of Specifically, addressing the use case in your original post @mikemaccana, codegenerated clients now have their own specialized version of If there's anything more to do here, specifically, feel free to describe it in a new issue! |
@steveluscher you are a true gent. i am curious tho, how would you approach network related error handling vs program error handling? most of the variable conditions at the rpc & network layer tend to cause the most issues within the code and are relevant for the UI to handle gracefully |
Using try {
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
} catch(e) {
if (isSolanaError(e, SOLANA_ERROR__RPC__TRANSPORT_HTTP_ERROR)) {
console.error('o no bad network', e.cause);
// Do something specific about an HTTP error, like retry.
} else {
throw e;
}
} |
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. |
Motivation
A user is using web3.js, making transactions with instructions for the
Token
program. They recieve:Which actually means, per the Token program's errors:
Example use case
This is a frequent show stopper for developers we've seen at Hacker Houses, where someone assumes there's no way to find an actual error. As a short term solution, DevRel added https://github.com/solana-developers/helpers?tab=readme-ov-file#getcustomerrormessage to our helpers library, but the same or better (and hopefully better is possible) solution should be available out of the box.
Details
Worst case: just have something like https://github.com/solana-developers/helpers?tab=readme-ov-file#getcustomerrormessage, code is at https://github.com/solana-developers/helpers/blob/main/src/index.ts#L14
Ideally: web3.js can dynamically fetch the errors for the specific program, and actually resolve the hex code to the real error message from the program as needed.
The text was updated successfully, but these errors were encountered: