carbon_core/
error.rs

1//! Defines the `Error` enum and `CarbonResult` type used for error handling in
2//! the `carbon-core` framework.
3//!
4//! The `Error` enum captures various error types that can occur within the
5//! framework, providing detailed error messages and support for custom error
6//! handling. The `CarbonResult` type alias simplifies function signatures by
7//! unifying the return type for functions that may return an `Error`.
8//!
9//! # Overview
10//!
11//! - **`Error`**: An enum representing specific error cases, from missing data
12//!   in transactions to issues with data sources. Each variant provides a
13//!   descriptive error message.
14//! - **`CarbonResult`**: A type alias for `Result<T, Error>`, where `T` is the
15//!   successful return type.
16//!
17//! These errors are essential for handling various scenarios that may arise
18//! during data processing in the `carbon-core` pipeline, including missing
19//! update types, missing transaction components, and custom errors for more
20//! flexible error management.
21//!
22//! # Notes
23//!
24//! - Implementing `thiserror::Error` provides automatic derivation of error
25//!   display messages.
26//! - Each error variant corresponds to a unique error scenario within the
27//!   `carbon-core` framework.
28
29use {crate::datasource::UpdateType, thiserror::Error};
30
31#[derive(Error, Debug)]
32pub enum Error {
33    #[error("Missing update type in datasource")]
34    MissingUpdateTypeInDatasource(UpdateType),
35    #[error("Failed to receive updates({0})")]
36    FailedToReceiveUpdates(String),
37    #[error("Transaction missing fee payer")]
38    MissingFeePayer,
39    #[error("Missing inner instructions")]
40    MissingInnerInstructions,
41    #[error("Missing account in transaction")]
42    MissingAccountInTransaction,
43    #[error("Missing instruction data")]
44    MissingInstructionData,
45    #[error("Failed to consume datasource ({0})")]
46    FailedToConsumeDatasource(String),
47    #[error("Custom error: {0}")]
48    Custom(String),
49}
50
51/// A type alias for `Result` with the `Error` type as the error variant.
52///
53/// This alias simplifies function signatures in the `carbon-core` framework by
54/// unifying error handling under a common type. Any function that may result in
55/// an `Error` can return a `CarbonResult`, providing clear and consistent error
56/// reporting.
57///
58/// # Example
59///
60/// ```rust
61///
62/// fn example_function(success: bool) -> CarbonResult<()> {
63///     if success {
64///         Ok(())
65///     } else {
66///         Err(Error::MissingInstructionData)
67///     }
68/// }
69///
70/// match example_function(false) {
71///     Ok(_) => println!("Operation succeeded."),
72///     Err(e) => eprintln!("Error occurred: {}", e),
73/// }
74/// ```
75pub type CarbonResult<T> = Result<T, Error>;