Custom Errors
Learn how to implement custom error handling in Anchor programs.
All instruction handlers in Anchor programs return a custom Result<T>
type
that allows you to handle successful execution with Ok(T)
and error cases with
Err(Error)
.
The
Result<T>
type in Anchor programs is a type alias that wraps the standard Rust
Result<T, E>
. In this case, T
represents the successful return type, while
E
is Anchor's custom Error
type.
Anchor Error
When an error occurs in an Anchor program, it returns a custom
Error
type defined as:
The Error
type in Anchor programs can be one of two variants:
ProgramErrorWithOrigin
: Custom type that wraps a standard SolanaProgramError
type. These errors come from thesolana_program
crate.
AnchorError
: Errors defined by the Anchor framework.
An AnchorError
can be thought of as having two categories:
-
Internal Anchor Errors - These are built-in errors included with the Anchor framework. They are defined in the
ErrorCode
enum. -
Custom Program Errors - These are program specific errors that developers define to handle custom error cases.
The error_code_number
from an AnchorError
has the following numbering
scheme:
Error Code | Description |
---|---|
>= 100 | Instruction error codes |
>= 1000 | IDL error codes |
>= 2000 | Constraint error codes |
>= 3000 | Account error codes |
>= 4100 | Misc error codes |
= 5000 | Deprecated error code |
>= 6000 | Starting point for custom user errors |
Usage
Anchor provides a convenient way to define custom errors through the
error_code
attribute. The implementation details can be found
here.
When you define an enum with the error_code
attribute, Anchor automatically:
- Assigns an error code starting from 6000
- Generates the necessary boilerplate for error handling
- Enables the use of custom error messages via the
msg
attribute
err!
To throw an error, use the
err!
macro. The err!
macro provides a convenient way to return custom errors from
your program. Under the hood, err!
uses the error!
macro to construct
AnchorError
. The implementation can be found
here.
require!
The
require!
macro provides a more concise way to handle error conditions. It combines a
condition check with returning an error if the condition is false. Here's how we
can rewrite the previous example using require!
:
Anchor provides several "require" macros for different validation needs. You can find the implementation of these macros here.
Macro | Description |
---|---|
require! | Ensures a condition is true, otherwise returns with the given error. |
require_eq! | Ensures two NON-PUBKEY values are equal. |
require_neq! | Ensures two NON-PUBKEY values are not equal. |
require_keys_eq! | Ensures two pubkeys values are equal. |
require_keys_neq! | Ensures two pubkeys are not equal. |
require_gt! | Ensures the first NON-PUBKEY value is greater than the second NON-PUBKEY value. |
require_gte! | Ensures the first NON-PUBKEY value is greater than or equal to the second NON-PUBKEY value. |
Example
Here's a simple example demonstrating how to define and handle custom errors in an Anchor program. The program below validates that an input amount falls within an acceptable range, showing how to:
- Define custom error types with messages
- Use the
require!
macro to check conditions and return errors
When a program error occurs, Anchor's TypeScript Client SDK returns a detailed error response containing information about the error. Here's an example error response showing the structure and available fields:
For a more comprehensive example, you can also reference the errors test program in the Anchor repository.