@solana/errors 2.0.0-canary-20240731173615
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +90 -0
- package/bin/cli.mjs +7 -0
- package/dist/cli.mjs +557 -0
- package/dist/index.browser.cjs +1046 -0
- package/dist/index.browser.cjs.map +1 -0
- package/dist/index.browser.mjs +819 -0
- package/dist/index.browser.mjs.map +1 -0
- package/dist/index.native.mjs +819 -0
- package/dist/index.native.mjs.map +1 -0
- package/dist/index.node.cjs +1046 -0
- package/dist/index.node.cjs.map +1 -0
- package/dist/index.node.mjs +819 -0
- package/dist/index.node.mjs.map +1 -0
- package/dist/types/codes.d.ts +266 -0
- package/dist/types/codes.d.ts.map +1 -0
- package/dist/types/context.d.ts +375 -0
- package/dist/types/context.d.ts.map +1 -0
- package/dist/types/error.d.ts +15 -0
- package/dist/types/error.d.ts.map +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/instruction-error.d.ts +5 -0
- package/dist/types/instruction-error.d.ts.map +1 -0
- package/dist/types/json-rpc-error.d.ts +52 -0
- package/dist/types/json-rpc-error.d.ts.map +1 -0
- package/dist/types/message-formatter.d.ts +4 -0
- package/dist/types/message-formatter.d.ts.map +1 -0
- package/dist/types/messages.d.ts +12 -0
- package/dist/types/messages.d.ts.map +1 -0
- package/dist/types/rpc-enum-errors.d.ts +31 -0
- package/dist/types/rpc-enum-errors.d.ts.map +1 -0
- package/dist/types/stack-trace.d.ts +2 -0
- package/dist/types/stack-trace.d.ts.map +1 -0
- package/dist/types/transaction-error.d.ts +5 -0
- package/dist/types/transaction-error.d.ts.map +1 -0
- package/package.json +83 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2023 Solana Labs, Inc
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
[![npm][npm-image]][npm-url]
|
|
2
|
+
[![npm-downloads][npm-downloads-image]][npm-url]
|
|
3
|
+
[![semantic-release][semantic-release-image]][semantic-release-url]
|
|
4
|
+
<br />
|
|
5
|
+
[![code-style-prettier][code-style-prettier-image]][code-style-prettier-url]
|
|
6
|
+
|
|
7
|
+
[code-style-prettier-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square
|
|
8
|
+
[code-style-prettier-url]: https://github.com/prettier/prettier
|
|
9
|
+
[npm-downloads-image]: https://img.shields.io/npm/dm/@solana/errors/experimental.svg?style=flat
|
|
10
|
+
[npm-image]: https://img.shields.io/npm/v/@solana/errors/experimental.svg?style=flat
|
|
11
|
+
[npm-url]: https://www.npmjs.com/package/@solana/errors/v/experimental
|
|
12
|
+
[semantic-release-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
|
|
13
|
+
[semantic-release-url]: https://github.com/semantic-release/semantic-release
|
|
14
|
+
|
|
15
|
+
# @solana/errors
|
|
16
|
+
|
|
17
|
+
This package brings together every error message across all Solana JavaScript modules.
|
|
18
|
+
|
|
19
|
+
## Reading error messages
|
|
20
|
+
|
|
21
|
+
### In development mode
|
|
22
|
+
|
|
23
|
+
When your bundler sets the constant `__DEV__` to `true`, every error message will be included in the bundle. As such, you will be able to read them in plain language wherever they appear.
|
|
24
|
+
|
|
25
|
+
> [!WARNING]
|
|
26
|
+
> The size of your JavaScript bundle will increase significantly with the inclusion of every error message in development mode. Be sure to build your bundle with `__DEV__` set to `false` when you go to production.
|
|
27
|
+
|
|
28
|
+
### In production mode
|
|
29
|
+
|
|
30
|
+
When your bundler sets the constant `__DEV__` to `false`, error messages will be stripped from the bundle to save space. Only the error code will appear when an error is encountered. Follow the instructions in the error message to convert the error code back to the human-readable error message.
|
|
31
|
+
|
|
32
|
+
For instance, to recover the error text for the error with code `123`:
|
|
33
|
+
|
|
34
|
+
```shell
|
|
35
|
+
npx @solana/errors decode -- 123
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Adding a new error
|
|
39
|
+
|
|
40
|
+
1. Add a new exported error code constant to `src/codes.ts`.
|
|
41
|
+
2. Add that new constant to the `SolanaErrorCode` union in `src/codes.ts`.
|
|
42
|
+
3. If you would like the new error to encapsulate context about the error itself (eg. the public keys for which a transaction is missing signatures) define the shape of that context in `src/context.ts`.
|
|
43
|
+
4. Add the error's message to `src/messages.ts`. Any context values that you defined above will be interpolated into the message wherever you write `$key`, where `key` is the index of a value in the context (eg. ``'Missing a signature for account `$address`'``).
|
|
44
|
+
5. Publish a new version of `@solana/errors`.
|
|
45
|
+
6. Bump the version of `@solana/errors` in the package from which the error is thrown.
|
|
46
|
+
|
|
47
|
+
## Removing an error message
|
|
48
|
+
|
|
49
|
+
- Don't remove errors.
|
|
50
|
+
- Don't change the meaning of an error message.
|
|
51
|
+
- Don't change or reorder error codes.
|
|
52
|
+
- Don't change or remove members of an error's context.
|
|
53
|
+
|
|
54
|
+
When an older client throws an error, we want to make sure that they can always decode the error. If you make any of the changes above, old clients will, by definition, not have received your changes. This could make the errors that they throw impossible to decode going forward.
|
|
55
|
+
|
|
56
|
+
## Catching errors
|
|
57
|
+
|
|
58
|
+
When you catch a `SolanaError` and assert its error code using `isSolanaError()`, TypeScript will refine the error's context to the type associated with that error code. You can use that context to render useful error messages, or to make context-aware decisions that help your application to recover from the error.
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import {
|
|
62
|
+
SOLANA_ERROR__TRANSACTION__MISSING_SIGNATURE,
|
|
63
|
+
SOLANA_ERROR__TRANSACTION__FEE_PAYER_SIGNATURE_MISSING,
|
|
64
|
+
isSolanaError,
|
|
65
|
+
} from '@solana/errors';
|
|
66
|
+
import { assertTransactionIsFullySigned, getSignatureFromTransaction } from '@solana/transactions';
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
const transactionSignature = getSignatureFromTransaction(tx);
|
|
70
|
+
assertTransactionIsFullySigned(tx);
|
|
71
|
+
/* ... */
|
|
72
|
+
} catch (e) {
|
|
73
|
+
if (isSolanaError(e, SOLANA_ERROR__TRANSACTION__SIGNATURES_MISSING)) {
|
|
74
|
+
displayError(
|
|
75
|
+
"We can't send this transaction without signatures for these addresses:\n- %s",
|
|
76
|
+
// The type of the `context` object is now refined to contain `addresses`.
|
|
77
|
+
e.context.addresses.join('\n- '),
|
|
78
|
+
);
|
|
79
|
+
return;
|
|
80
|
+
} else if (isSolanaError(e, SOLANA_ERROR__TRANSACTION__FEE_PAYER_SIGNATURE_MISSING)) {
|
|
81
|
+
if (!tx.feePayer) {
|
|
82
|
+
displayError('Choose a fee payer for this transaction before sending it');
|
|
83
|
+
} else {
|
|
84
|
+
displayError('The fee payer still needs to sign for this transaction');
|
|
85
|
+
}
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
throw e;
|
|
89
|
+
}
|
|
90
|
+
```
|