Skip to content

Commit

Permalink
Organize error code
Browse files Browse the repository at this point in the history
  • Loading branch information
temeddix committed Sep 29, 2024
1 parent 377eb22 commit 3a04b86
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
25 changes: 17 additions & 8 deletions flutter_package/example/native/sample_crate/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@ use std::error::Error;
use std::fmt;

#[derive(Debug)]
pub struct ExampleError(pub Box<dyn Error + Send + Sync>);
pub enum ExampleError {
Fractal,
HardwareId,
WebApi,
}

impl fmt::Display for ExampleError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let source = self.0.as_ref();
write!(f, "An error occured inside the example code.\n{source}")
match self {
Self::Fractal => {
write!(f, "Failed to generate fractal")
}
Self::HardwareId => {
write!(f, "Unable to retrieve hardware ID")
}
Self::WebApi => {
write!(f, "Web API call failed")
}
}
}
}

impl Error for ExampleError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(self.0.as_ref())
}
}
impl Error for ExampleError {}
2 changes: 1 addition & 1 deletion flutter_package/example/native/sample_crate/src/fractal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn draw_fractal_image(scale: f64) -> Result<Vec<u8>, ExampleError> {

match result {
Ok(_) => Ok(image_data),
Err(error) => Err(ExampleError(error.into())),
Err(_) => Err(ExampleError::Fractal),
}
}

Expand Down
6 changes: 3 additions & 3 deletions flutter_package/example/native/sample_crate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn get_hardward_id() -> Result<String, ExampleError> {
.add_component(machineid_rs::HWIDComponent::CPUCores);
let hwid = builder
.build("mykey")
.map_err(|error| ExampleError(error.into()))?;
.map_err(|_| ExampleError::HardwareId)?;
Ok(hwid)
}
#[cfg(not(any(
Expand All @@ -39,9 +39,9 @@ pub fn get_current_time() -> DateTime<offset::Local> {
pub async fn fetch_from_web_api(url: &str) -> Result<String, ExampleError> {
let fetched = reqwest::get(url)
.await
.map_err(|error| ExampleError(error.into()))?
.map_err(|_| ExampleError::WebApi)?
.text()
.await
.map_err(|error| ExampleError(error.into()))?;
.map_err(|_| ExampleError::WebApi)?;
Ok(fetched)
}
12 changes: 6 additions & 6 deletions rust_crate/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ pub enum RinfError {
impl fmt::Display for RinfError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RinfError::NoDartIsolate => {
write!(f, "Dart isolate for Rust signals was not created.")
Self::NoDartIsolate => {
write!(f, "Dart isolate for Rust signals was not created")
}
RinfError::CannotDecodeMessage => {
write!(f, "Could not decode the message.")
Self::CannotDecodeMessage => {
write!(f, "Could not decode the message")
}
RinfError::NoSignalHandler => {
write!(f, "Could not find the handler for Dart signal.")
Self::NoSignalHandler => {
write!(f, "Could not find the handler for Dart signal")
}
}
}
Expand Down

0 comments on commit 3a04b86

Please sign in to comment.