ErrorCode Macro
The ErrorCode macro simplifies adding numeric error codes to your Rust enums. It automatically generates an error_code method that returns the error code associated with each enum variant. This macro is designed to be easy to use while offering flexibility for more anced needs.
Table of Contents
Github
Basic Usage
The simplest way to use the ErrorCode macro requires just a few steps:
- Add the
error_codecrate to yourCargo.toml. - Apply
#[derive(ErrorCode)]to your enum. - Assign an error code to each variant using
#[error_code(value)].
At its most basic, every variant must have an explicitly defined error code, and the error code type defaults to u32.
Example: Basic Usage
Here’s a minimal example of defining an error enum with error codes:
use ErrorCode;
In this example:
MyError::InvalidInputhas an error code of100.MyError::NotFoundhas an error code of200.
You can then use the error_code method to retrieve these codes:
let error = InvalidInput;
println!; // Outputs: "100"
Advanced Usage
For more control, the ErrorCode macro supports customizing the error code type and providing a default error code for variants that don’t specify one.
Customizing the Error Code Type
By default, error codes are u32. You can change this to another unsigned integer type (e.g., u8, u16, u64) using the type attribute at the enum level.
- Syntax:
#[error_code(type = "u16")] - Default: If
typeis not specified, it’su32.
Setting a Default Error Code
You can define a default error code for variants that lack an explicit #[error_code(value)] using the default attribute.
- Syntax:
#[error_code(default = 300)]
Example: Advanced Usage
Here’s an example that uses both type and default:
use ErrorCode;
In this example:
- The error code type is
u16instead of the defaultu32. MyError::InvalidInputhas an explicit error code of100.MyError::NotFounduses the default error code of300since it lacks an explicit value.
Using the error_code method:
let error1 = InvalidInput;
println!; // Outputs: "100"
let error2 = NotFound;
println!; // Outputs: "300"
Key Points:
- If you don’t set
type, it defaults tou32.
Compatibility with thiserror
The ErrorCode macro works seamlessly with thiserror, allowing you to combine rich error messages with numeric error codes. This is especially useful when you want both human-readable error details and machine-readable codes.
Example: Using ErrorCode with thiserror
Here’s an example that integrates both macros:
use ErrorCode;
use Error;
use Error as IoError;
In this example:
thiserrorprovides descriptive error messages.ErrorCodeadds numeric codes.IoErroruses the default code (300) since it doesn’t have an explicit#[error_code(value)].
Using both features:
let error = InvalidInput;
println!; // Outputs: "Invalid input: test"
println!; // Outputs: "100"
let io_error = new;
let error = IoError;
println!; // Outputs: "io failure"
println!; // Outputs: "300"
Why It Works Well:
ErrorCodeandthiserrorcomplement each other without overlap.- You can use
ErrorCodealone for codes,thiserroralone for messages, or both together, giving you full flexibility.