Standardise error handling #57

Closed
opened 2024-04-21 16:04:57 +01:00 by kemitix · 1 comment
Owner

Ref: Rust Error Handling - Best Practices
(Jeremy Chone )

Error type for modules that require them.

my_module::Error

Don't create an error module.

The my_module::Error should be an enum.

As a sibling to the enum Error create a Result type:

pub type Result<T> = core::result::Result<T, Error>;

For tests, can use an error type:

pub type Error = Box<dyn std::error::Error>;

Look at crate derive_more, to allow use of:

#[derive(Debug, From, Display)]
pub enum Error {
  #[display("Foo: '{_0}'")]
  #[from]
  Foo(foo::Error)
}
derive_more = { version = "1.0.0.-beta", features = [ "from", "display" ] }

Use struct for enum variantes:

pub enum Error {
  LimitToHigh {
    actual: i64,
    max: i64,
  }
}

Example template:

use derive_mod::From;

pub type Result<T> = core::result::Result<T, Error>;

#[derive(Debug, From)]
pub enum Error {
  // Use early in dev, then remove
  #[from]
  Custom(String),

  // fs
  FsDescriptiveErrorEnum.

  #[from]
  Io(std::io::Error),
}

// Custom begin
impl Error {
  pub fn custom(val: impl std::fmt::Display) -> Self {
    Self::Custom(val.to_string())
  }
}

impl From<&str> for Error {
  fn from(val: &str) -> Self {
    Self::Custom(val.to_string())
  }
}
// Custom end

impl core::fmt::Display for Error {
  fn fmt(&self, fmt: &fmt core::fmt::Formatter) -> core::result::Result<(), core::...??? {
    write!(fmt, "{self:?}")
  }
}

impl std::error::Error for Error {}
Ref: [Rust Error Handling - Best Practices ](https://tube.kemitix.net/watch?v=j-VQCYP7wyw) (Jeremy Chone ) `Error` type for modules that require them. ```rust my_module::Error ``` Don't create an `error` module. The `my_module::Error` should be an `enum`. As a sibling to the `enum Error` create a `Result` type: ```rust pub type Result<T> = core::result::Result<T, Error>; ``` For tests, can use an error type: ```rust pub type Error = Box<dyn std::error::Error>; ``` Look at crate `derive_more`, to allow use of: ```rust #[derive(Debug, From, Display)] pub enum Error { #[display("Foo: '{_0}'")] #[from] Foo(foo::Error) } ``` ```toml derive_more = { version = "1.0.0.-beta", features = [ "from", "display" ] } ``` Use struct for `enum` variantes: ```rust pub enum Error { LimitToHigh { actual: i64, max: i64, } } ``` Example template: ```rust use derive_mod::From; pub type Result<T> = core::result::Result<T, Error>; #[derive(Debug, From)] pub enum Error { // Use early in dev, then remove #[from] Custom(String), // fs FsDescriptiveErrorEnum. #[from] Io(std::io::Error), } // Custom begin impl Error { pub fn custom(val: impl std::fmt::Display) -> Self { Self::Custom(val.to_string()) } } impl From<&str> for Error { fn from(val: &str) -> Self { Self::Custom(val.to_string()) } } // Custom end impl core::fmt::Display for Error { fn fmt(&self, fmt: &fmt core::fmt::Formatter) -> core::result::Result<(), core::...??? { write!(fmt, "{self:?}") } } impl std::error::Error for Error {} ```
Author
Owner

Define Result and Error withing tests module:

#[cfg(test)]
mod tests {
  type Error = Box<dyn std::error::Error>;
  type Result<T> = core::result::Result<T, Error>;

  #[test]
  fn test_name() -> Result<()> {
    // ...
  }
}
Define Result and Error withing tests module: ```rust #[cfg(test)] mod tests { type Error = Box<dyn std::error::Error>; type Result<T> = core::result::Result<T, Error>; #[test] fn test_name() -> Result<()> { // ... } } ```
kemitix added this to the Road to v1 - Forgejo & Github project 2024-07-16 06:58:05 +01:00
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: kemitix/git-next#57
No description provided.