Errors
Recover from errors in development and production
Introduction
The ecosystem of packages based on Kit's error system benefit from strongly typed error data, and descriptive error messages. Its error messages get scrubbed from your production bundles, which means that the library of error messages can grow indefinitely without adding a single byte to your app.
Installation
Errors are included within the @solana/kit
library but you may also install them using their standalone package.
What is a SolanaError
?
A SolanaError
is a class made of three components:
- An error code; found in the union named
SolanaErrorCode
- An error message
- Optional error context
Here's an example of some code that responds to various errors, taking advantage of the error context offered by each one:
Catching errors
When you encounter an error, you can determine whether it is a SolanaError
or not using the isSolanaError()
function:
When you have a strategy for handling a particular error, you can supply that error's code as the second argument. This will both identify that error by code and refine the type of the SolanaError
such that the shape of its context
property becomes known to TypeScript.
Some Solana errors have, as their root cause, another Solana error. One such example is during transaction simulation (preflight):
Error messages
In development mode
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.
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.
In production mode
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 in the message when an error is encountered. Follow the instructions in the error message to convert the error code back to the human-readable error message.
For instance, to recover the error text for the error with code 7050005
: