Bladeren bron

Feat/integrate thiserror anyhow (#1599)

* feat: add thiserror and anyhow dependencies

* feat: add thiserror derive to ErrorCode

* feat: add thiserror derive to FlowyStruct

* feat: update error field

* feat: update lock files

* Revert "feat: update lock files"

This reverts commit df9736b8435a0dfb6c8ec2e831631fa24c57d0dc.

* fix: cargo fmt
Iñigo Iparragirre 2 jaren geleden
bovenliggende
commit
8139856d6d

+ 2 - 0
frontend/rust-lib/flowy-error/Cargo.toml

@@ -11,6 +11,8 @@ flowy-error-code = { path = "../../../shared-lib/flowy-error-code"}
 lib-dispatch = { path = "../lib-dispatch" }
 protobuf = {version = "2.20.0"}
 bytes = "1.0"
+anyhow = "1.0"
+thiserror = "1.0"
 
 
 flowy-sync = { path = "../../../shared-lib/flowy-sync", optional = true}

+ 10 - 6
frontend/rust-lib/flowy-error/src/errors.rs

@@ -1,12 +1,15 @@
+use anyhow::Result;
 use bytes::Bytes;
 use flowy_derive::ProtoBuf;
 use flowy_error_code::ErrorCode;
 use lib_dispatch::prelude::{AFPluginEventResponse, ResponseBuilder};
-use std::{convert::TryInto, fmt, fmt::Debug};
+use std::{convert::TryInto, fmt::Debug};
+use thiserror::Error;
 
-pub type FlowyResult<T> = std::result::Result<T, FlowyError>;
+pub type FlowyResult<T> = anyhow::Result<T, FlowyError>;
 
-#[derive(Debug, Default, Clone, ProtoBuf)]
+#[derive(Debug, Default, Clone, ProtoBuf, Error)]
+#[error("{code:?}: {msg}")]
 pub struct FlowyError {
     #[pb(index = 1)]
     pub code: i32,
@@ -87,12 +90,13 @@ where
     FlowyError::internal().context(e)
 }
 
-impl fmt::Display for FlowyError {
+// Not needed because of thiserror derive macro
+/* impl fmt::Display for FlowyError {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         write!(f, "{:?}: {}", &self.code, &self.msg)
     }
 }
-
+ */
 impl lib_dispatch::Error for FlowyError {
     fn as_response(&self) -> AFPluginEventResponse {
         let bytes: Bytes = self.clone().try_into().unwrap();
@@ -114,4 +118,4 @@ impl std::convert::From<protobuf::ProtobufError> for FlowyError {
     }
 }
 
-impl std::error::Error for FlowyError {}
+//impl std::error::Error for FlowyError {}

+ 1 - 0
shared-lib/flowy-error-code/Cargo.toml

@@ -9,6 +9,7 @@ edition = "2018"
 flowy-derive = { path = "../flowy-derive" }
 protobuf = {version = "2.18.0"}
 derive_more = {version = "0.99", features = ["display"]}
+thiserror = "1.0"
 
 [build-dependencies]
 flowy-codegen= { path = "../flowy-codegen", features = ["proto_gen"]}

+ 54 - 54
shared-lib/flowy-error-code/src/code.rs

@@ -1,142 +1,142 @@
 use crate::protobuf::ErrorCode as ProtoBufErrorCode;
-use derive_more::Display;
 use flowy_derive::ProtoBuf_Enum;
 use protobuf::ProtobufEnum;
 use std::convert::{TryFrom, TryInto};
+use thiserror::Error;
 
-#[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
+#[derive(Debug, Clone, ProtoBuf_Enum, PartialEq, Eq, Error)]
 pub enum ErrorCode {
-    #[display(fmt = "Internal error")]
+    #[error("Internal error")]
     Internal = 0,
 
-    #[display(fmt = "UserUnauthorized")]
+    #[error("UserUnauthorized")]
     UserUnauthorized = 2,
 
-    #[display(fmt = "RecordNotFound")]
+    #[error("RecordNotFound")]
     RecordNotFound = 3,
 
-    #[display(fmt = "User id is empty")]
+    #[error("User id is empty")]
     UserIdIsEmpty = 4,
 
-    #[display(fmt = "Workspace name can not be empty or whitespace")]
+    #[error("Workspace name can not be empty or whitespace")]
     WorkspaceNameInvalid = 100,
 
-    #[display(fmt = "Workspace id can not be empty or whitespace")]
+    #[error("Workspace id can not be empty or whitespace")]
     WorkspaceIdInvalid = 101,
 
-    #[display(fmt = "Color style of the App is invalid")]
+    #[error("Color style of the App is invalid")]
     AppColorStyleInvalid = 102,
 
-    #[display(fmt = "Workspace desc is invalid")]
+    #[error("Workspace desc is invalid")]
     WorkspaceDescTooLong = 103,
 
-    #[display(fmt = "Workspace description too long")]
+    #[error("Workspace description too long")]
     WorkspaceNameTooLong = 104,
 
-    #[display(fmt = "App id can not be empty or whitespace")]
+    #[error("App id can not be empty or whitespace")]
     AppIdInvalid = 110,
 
-    #[display(fmt = "App name can not be empty or whitespace")]
+    #[error("App name can not be empty or whitespace")]
     AppNameInvalid = 111,
 
-    #[display(fmt = "View name can not be empty or whitespace")]
+    #[error("View name can not be empty or whitespace")]
     ViewNameInvalid = 120,
 
-    #[display(fmt = "Thumbnail of the view is invalid")]
+    #[error("Thumbnail of the view is invalid")]
     ViewThumbnailInvalid = 121,
 
-    #[display(fmt = "View id can not be empty or whitespace")]
+    #[error("View id can not be empty or whitespace")]
     ViewIdInvalid = 122,
 
-    #[display(fmt = "View desc too long")]
+    #[error("View desc too long")]
     ViewDescTooLong = 123,
 
-    #[display(fmt = "View data is invalid")]
+    #[error("View data is invalid")]
     ViewDataInvalid = 124,
 
-    #[display(fmt = "View name too long")]
+    #[error("View name too long")]
     ViewNameTooLong = 125,
 
-    #[display(fmt = "Connection error")]
+    #[error("Connection error")]
     ConnectError = 200,
 
-    #[display(fmt = "Email can not be empty or whitespace")]
+    #[error("Email can not be empty or whitespace")]
     EmailIsEmpty = 300,
-    #[display(fmt = "Email format is not valid")]
+    #[error("Email format is not valid")]
     EmailFormatInvalid = 301,
-    #[display(fmt = "Email already exists")]
+    #[error("Email already exists")]
     EmailAlreadyExists = 302,
-    #[display(fmt = "Password can not be empty or whitespace")]
+    #[error("Password can not be empty or whitespace")]
     PasswordIsEmpty = 303,
-    #[display(fmt = "Password format too long")]
+    #[error("Password format too long")]
     PasswordTooLong = 304,
-    #[display(fmt = "Password contains forbidden characters.")]
+    #[error("Password contains forbidden characters.")]
     PasswordContainsForbidCharacters = 305,
-    #[display(fmt = "Password should contain a minimum of 6 characters with 1 special 1 letter and 1 numeric")]
+    #[error("Password should contain a minimum of 6 characters with 1 special 1 letter and 1 numeric")]
     PasswordFormatInvalid = 306,
-    #[display(fmt = "Password not match")]
+    #[error("Password not match")]
     PasswordNotMatch = 307,
-    #[display(fmt = "User name is too long")]
+    #[error("User name is too long")]
     UserNameTooLong = 308,
-    #[display(fmt = "User name contain forbidden characters")]
+    #[error("User name contain forbidden characters")]
     UserNameContainForbiddenCharacters = 309,
-    #[display(fmt = "User name can not be empty or whitespace")]
+    #[error("User name can not be empty or whitespace")]
     UserNameIsEmpty = 310,
-    #[display(fmt = "user id is empty or whitespace")]
+    #[error("user id is empty or whitespace")]
     UserIdInvalid = 311,
-    #[display(fmt = "User not exist")]
+    #[error("User not exist")]
     UserNotExist = 312,
-    #[display(fmt = "Text is too long")]
+    #[error("Text is too long")]
     TextTooLong = 400,
 
-    #[display(fmt = "Grid id is empty")]
+    #[error("Grid id is empty")]
     GridIdIsEmpty = 410,
-    #[display(fmt = "Grid view id is empty")]
+    #[error("Grid view id is empty")]
     GridViewIdIsEmpty = 411,
 
-    #[display(fmt = "Grid block id is empty")]
+    #[error("Grid block id is empty")]
     BlockIdIsEmpty = 420,
-    #[display(fmt = "Row id is empty")]
+    #[error("Row id is empty")]
     RowIdIsEmpty = 430,
-    #[display(fmt = "Select option id is empty")]
+    #[error("Select option id is empty")]
     OptionIdIsEmpty = 431,
-    #[display(fmt = "Field id is empty")]
+    #[error("Field id is empty")]
     FieldIdIsEmpty = 440,
-    #[display(fmt = "Field doesn't exist")]
+    #[error("Field doesn't exist")]
     FieldDoesNotExist = 441,
-    #[display(fmt = "The name of the option should not be empty")]
+    #[error("The name of the option should not be empty")]
     SelectOptionNameIsEmpty = 442,
-    #[display(fmt = "Field not exists")]
+    #[error("Field not exists")]
     FieldNotExists = 443,
-    #[display(fmt = "The operation in this field is invalid")]
+    #[error("The operation in this field is invalid")]
     FieldInvalidOperation = 444,
-    #[display(fmt = "Filter id is empty")]
+    #[error("Filter id is empty")]
     FilterIdIsEmpty = 445,
-    #[display(fmt = "Field is not exist")]
+    #[error("Field is not exist")]
     FieldRecordNotFound = 446,
 
-    #[display(fmt = "Field's type-option data should not be empty")]
+    #[error("Field's type-option data should not be empty")]
     TypeOptionDataIsEmpty = 450,
 
-    #[display(fmt = "Group id is empty")]
+    #[error("Group id is empty")]
     GroupIdIsEmpty = 460,
 
-    #[display(fmt = "Invalid date time format")]
+    #[error("Invalid date time format")]
     InvalidDateTimeFormat = 500,
 
-    #[display(fmt = "The input string is empty or contains invalid characters")]
+    #[error("The input string is empty or contains invalid characters")]
     UnexpectedEmptyString = 999,
 
-    #[display(fmt = "Invalid data")]
+    #[error("Invalid data")]
     InvalidData = 1000,
 
-    #[display(fmt = "Serde")]
+    #[error("Serde")]
     Serde = 1001,
 
-    #[display(fmt = "Protobuf serde")]
+    #[error("Protobuf serde")]
     ProtobufSerde = 1002,
 
-    #[display(fmt = "Out of bounds")]
+    #[error("Out of bounds")]
     OutOfBounds = 10001,
 }