From 3a04b86cbb26e9000068cd21698a88b603f50739 Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Sun, 29 Sep 2024 22:46:50 +0900 Subject: [PATCH] Organize error code --- .../example/native/sample_crate/src/error.rs | 25 +++++++++++++------ .../native/sample_crate/src/fractal.rs | 2 +- .../example/native/sample_crate/src/lib.rs | 6 ++--- rust_crate/src/error.rs | 12 ++++----- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/flutter_package/example/native/sample_crate/src/error.rs b/flutter_package/example/native/sample_crate/src/error.rs index 94fd4ff8..6eb7dae6 100644 --- a/flutter_package/example/native/sample_crate/src/error.rs +++ b/flutter_package/example/native/sample_crate/src/error.rs @@ -2,17 +2,26 @@ use std::error::Error; use std::fmt; #[derive(Debug)] -pub struct ExampleError(pub Box); +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 {} diff --git a/flutter_package/example/native/sample_crate/src/fractal.rs b/flutter_package/example/native/sample_crate/src/fractal.rs index 12ad1423..0b52b8c2 100644 --- a/flutter_package/example/native/sample_crate/src/fractal.rs +++ b/flutter_package/example/native/sample_crate/src/fractal.rs @@ -29,7 +29,7 @@ pub fn draw_fractal_image(scale: f64) -> Result, ExampleError> { match result { Ok(_) => Ok(image_data), - Err(error) => Err(ExampleError(error.into())), + Err(_) => Err(ExampleError::Fractal), } } diff --git a/flutter_package/example/native/sample_crate/src/lib.rs b/flutter_package/example/native/sample_crate/src/lib.rs index dd1af987..ef5b3fd9 100644 --- a/flutter_package/example/native/sample_crate/src/lib.rs +++ b/flutter_package/example/native/sample_crate/src/lib.rs @@ -17,7 +17,7 @@ pub fn get_hardward_id() -> Result { .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( @@ -39,9 +39,9 @@ pub fn get_current_time() -> DateTime { pub async fn fetch_from_web_api(url: &str) -> Result { 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) } diff --git a/rust_crate/src/error.rs b/rust_crate/src/error.rs index 489c8f86..14754371 100644 --- a/rust_crate/src/error.rs +++ b/rust_crate/src/error.rs @@ -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") } } }