Struct Error

Source
pub struct Error {
    pub kind: ErrorKind,
    pub class: String,
    pub message: String,
    pub details: Option<BTreeMap<String, Value>>,
}
Expand description

A structured error type with categorized information.

The Error struct represents an error with a specific kind, classification, message, and optional additional details.

This structure is designed to facilitate error handling by providing detailed information that can be logged or displayed.

Fields§

§kind: ErrorKind

The kind of error.

This field categorizes the error, allowing distinct handling based on its type. It is skipped during serialization.

§class: String

The class or category of the error.

This helps further classify the error beyond its kind.

§message: String

A human-readable message describing the error.

§details: Option<BTreeMap<String, Value>>

Additional details related to the error.

This optional field contains extra context in a key-value format, which can be useful for debugging or logging purposes.

Trait Implementations§

Source§

impl Clone for Error

Source§

fn clone(&self) -> Error

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Error

Source§

fn default() -> Self

Creates a default instance of Error.

This implementation of the Default trait provides a default error of type Error, with predefined values:

  • Kind: Represents an internal server error (InternalServerError, MSG000, 500, “Internal Server Error”).
  • Class: Describes the error as a server-side internal error (Server::InternalServerError::Error).
  • Message: The human-readable error message (“Internal Server Error”).
  • Details: No additional error details are provided (None).

This can be used when you need a generic error with standard values.

§Example
let error: cdumay_error::Error = Default::default();
assert_eq!(error.kind.name(), "InternalServerError");
assert_eq!(error.message, "Internal Server Error");
assert_eq!(error.class, "Server::InternalServerError::Error");
assert!(error.details.is_none());
Source§

impl Display for Error

Implements the Display trait for Error.

This implementation formats the error as a human-readable string, including its kind, class, code, and message. It provides a structured error output that can be useful for logging or displaying errors in a UI.

§Format

[message_id] class (code) - message

§Example

let error = cdumay_error::Error {
    kind: cdumay_error::ErrorKind("NotFound", "MSG001", 404, "Not Found"),
    class: "Client::NotFound::MyError".to_string(),
    message: "Not Found".to_string(),
    details: None,
};
println!("{}", error);
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<E: AsError> From<E> for Error

Converts any type implementing AsError into an Error instance.

This implementation allows seamless conversion from custom error types that implement AsError into the Error struct, preserving structured error information.

§Type Parameters

  • E: A type that implements the AsError trait.

§Example

#[allow(non_upper_case_globals)]
pub const IoError: cdumay_error::ErrorKind = cdumay_error::ErrorKind(
    "IoError",
    "Input / output error",
    500,
    "The requested file raised error"
);
#[derive(Debug, Clone)]
pub struct NotFoundError {
    class: String,
    message: String,
    details: Option<std::collections::BTreeMap<String, serde_value::Value>>,
}
impl NotFoundError {
    pub const kind: cdumay_error::ErrorKind = IoError;

    pub fn new() -> Self {
        Self {
            class: format!("{}::{}::{}", Self::kind.side(), Self::kind.name(), "NotFoundError"),
            message: Self::kind.description().into(),
            details: None,
        }
    }
}
impl cdumay_error::AsError for NotFoundError {
    fn kind() -> cdumay_error::ErrorKind {
        Self::kind
    }
    fn class(&self) -> String {
        self.class.clone()
    }
    fn message(&self) -> String {
        self.message.clone()
    }
    fn details(&self) -> Option<std::collections::BTreeMap<String, serde_value::Value>> {
        self.details.clone()
    }
}
let custom_error = NotFoundError::new();
let error: cdumay_error::Error = custom_error.into();
Source§

fn from(value: E) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for Error

Converts an Error into a std::io::Error.

This implementation maps an Error to an std::io::Error using the InvalidData error kind and formats the error message accordingly. This allows for seamless integration with Rust’s standard I/O error handling.

§Example

let custom_error = cdumay_error::Error {
    kind: cdumay_error::ErrorKind("NotFound", "MSG001", 404, "Not Found"),
    class: "Client::NotFound::MyError".to_string(),
    message: "Not Found".to_string(),
    details: None,
};
let io_error: std::io::Error = custom_error.into();
Source§

fn from(e: Error) -> Self

Converts to this type from the input type.
Source§

impl Serialize for Error

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Error

§

impl RefUnwindSafe for Error

§

impl Send for Error

§

impl Sync for Error

§

impl Unpin for Error

§

impl UnwindSafe for Error

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.