rline_api 1.0.0

rline public API to create, manipulate, and convert rows of data, making it easy to work with datasets
Documentation
//! # rline_error
//!
//! The `rline_error` module defines custom error types for the `rline_api` library.
//!
//! ## Error Types
//!
//! - [`RlineError`](enum.RlineError.html): Represents any error that may occur during rline initialization and running.
//!
//!     - [`ConfigurationError`](enum.RlineError.html#variant.ConfigurationError): This exception is thrown whenever a required property is missing, or if a mismatch in your configuration is detected, like an invalid dag. It is typically caused by a typo or misconfiguration.
//!
//!     - [`RuntimeError`](enum.RlineError.html#variant.RuntimeError): A `RuntimeError` is raised whenever a connector encounters an error, such as an I/O or connection error. It is essential to provide a useful error message to the user and preserve the error trace when available.
//!
//!     - [`TooManyFailsError`](enum.RlineError.html#variant.TooManyFailsError): Thrown when the exit condition of the number of failing rows is reached. Indicates that a specified number of rows failed to process.
//!
//!     - [`BadTypeError`](enum.RlineError.html#variant.BadTypeError): Raised when attempting to cast a `Value` to an incompatible type.
//!
//! ## Examples
//!
//! ```rust
//! use rline_api::rline_error::RlineError;
//!
//! fn example_function() -> Result<(), RlineError> {
//!     // Simulate a configuration error
//!     Err(RlineError::ConfigurationError("Invalid configuration".to_string()))?;
//!
//!     // Simulate a runtime error
//!     Err(RlineError::RuntimeError("Failed to connect".to_string()))?;
//!
//!     Ok(())
//! }
//! ```
//!
//! ## Usage
//!
//! This module is used within the `rline_api` library to handle various error scenarios.
//!
//! ## License
//!
//! This module is part of the `rline_api` crate, licensed under the Apache-2.0 License.

use std::error::Error;
use std::fmt::{Debug, Display, Formatter};

#[derive(Debug, Clone)]
/// Represents any error that may occur during rline initialization and running.
pub enum RlineError {
    /// This exception is probably the most crucial rline error.
    /// It is thrown whenever a required property is missing, or if a mismatch in your configuration
    /// like invalid dag.
    /// You may have a typo somewhere.
    ConfigurationError(String),
    /// A RuntimeError is raised whenever a connector encounters an error.
    /// Typically an I/O or connection error.
    /// It is of highest importance to raise a useful error message to the user, and to keep
    /// the error trace when available.
    RuntimeError(String),
    /// A TooManyFailsError thrown whenever the exit condition of number of failing rows is reached.
    TooManyFailsError(usize),
    /// A RuntimeError is raised whenever you try to cast a Value to a bad type.
    BadTypeError(String),
}

impl Display for RlineError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            RlineError::ConfigurationError(cause) => write!(f, "ConfigurationError: {}", cause),
            RlineError::RuntimeError(cause) => write!(f, "RuntimeError: {}", cause),
            RlineError::BadTypeError(cause) => write!(f, "BadTypeError: {}", cause),
            RlineError::TooManyFailsError(number_fails) => {
                write!(
                    f,
                    "TooManyFailsError: {} {}",
                    number_fails,
                    if *number_fails > 1 { "fails" } else { "fail" }
                )
            }
        }
    }
}

impl Error for RlineError {}

impl RlineError {
    /// Returns a new RuntimeError keeping the trace of the causing error.
    pub fn runtime_error(cause: &dyn Error) -> Self {
        Self::RuntimeError(cause.to_string())
    }

    /// Returns a new ConfigurationError keeping the trace of the causing error.
    pub fn configuration_error(cause: &dyn Error) -> Self {
        Self::RuntimeError(cause.to_string())
    }
}