Skip to content

Commit 8139856

Browse files
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 df9736b. * fix: cargo fmt
1 parent b83b182 commit 8139856

File tree

4 files changed

+67
-60
lines changed

4 files changed

+67
-60
lines changed

frontend/rust-lib/flowy-error/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ flowy-error-code = { path = "../../../shared-lib/flowy-error-code"}
1111
lib-dispatch = { path = "../lib-dispatch" }
1212
protobuf = {version = "2.20.0"}
1313
bytes = "1.0"
14+
anyhow = "1.0"
15+
thiserror = "1.0"
1416

1517

1618
flowy-sync = { path = "../../../shared-lib/flowy-sync", optional = true}

frontend/rust-lib/flowy-error/src/errors.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
use anyhow::Result;
12
use bytes::Bytes;
23
use flowy_derive::ProtoBuf;
34
use flowy_error_code::ErrorCode;
45
use lib_dispatch::prelude::{AFPluginEventResponse, ResponseBuilder};
5-
use std::{convert::TryInto, fmt, fmt::Debug};
6+
use std::{convert::TryInto, fmt::Debug};
7+
use thiserror::Error;
68

7-
pub type FlowyResult<T> = std::result::Result<T, FlowyError>;
9+
pub type FlowyResult<T> = anyhow::Result<T, FlowyError>;
810

9-
#[derive(Debug, Default, Clone, ProtoBuf)]
11+
#[derive(Debug, Default, Clone, ProtoBuf, Error)]
12+
#[error("{code:?}: {msg}")]
1013
pub struct FlowyError {
1114
#[pb(index = 1)]
1215
pub code: i32,
@@ -87,12 +90,13 @@ where
8790
FlowyError::internal().context(e)
8891
}
8992

90-
impl fmt::Display for FlowyError {
93+
// Not needed because of thiserror derive macro
94+
/* impl fmt::Display for FlowyError {
9195
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9296
write!(f, "{:?}: {}", &self.code, &self.msg)
9397
}
9498
}
95-
99+
*/
96100
impl lib_dispatch::Error for FlowyError {
97101
fn as_response(&self) -> AFPluginEventResponse {
98102
let bytes: Bytes = self.clone().try_into().unwrap();
@@ -114,4 +118,4 @@ impl std::convert::From<protobuf::ProtobufError> for FlowyError {
114118
}
115119
}
116120

117-
impl std::error::Error for FlowyError {}
121+
//impl std::error::Error for FlowyError {}

shared-lib/flowy-error-code/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ edition = "2018"
99
flowy-derive = { path = "../flowy-derive" }
1010
protobuf = {version = "2.18.0"}
1111
derive_more = {version = "0.99", features = ["display"]}
12+
thiserror = "1.0"
1213

1314
[build-dependencies]
1415
flowy-codegen= { path = "../flowy-codegen", features = ["proto_gen"]}

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

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,142 @@
11
use crate::protobuf::ErrorCode as ProtoBufErrorCode;
2-
use derive_more::Display;
32
use flowy_derive::ProtoBuf_Enum;
43
use protobuf::ProtobufEnum;
54
use std::convert::{TryFrom, TryInto};
5+
use thiserror::Error;
66

7-
#[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
7+
#[derive(Debug, Clone, ProtoBuf_Enum, PartialEq, Eq, Error)]
88
pub enum ErrorCode {
9-
#[display(fmt = "Internal error")]
9+
#[error("Internal error")]
1010
Internal = 0,
1111

12-
#[display(fmt = "UserUnauthorized")]
12+
#[error("UserUnauthorized")]
1313
UserUnauthorized = 2,
1414

15-
#[display(fmt = "RecordNotFound")]
15+
#[error("RecordNotFound")]
1616
RecordNotFound = 3,
1717

18-
#[display(fmt = "User id is empty")]
18+
#[error("User id is empty")]
1919
UserIdIsEmpty = 4,
2020

21-
#[display(fmt = "Workspace name can not be empty or whitespace")]
21+
#[error("Workspace name can not be empty or whitespace")]
2222
WorkspaceNameInvalid = 100,
2323

24-
#[display(fmt = "Workspace id can not be empty or whitespace")]
24+
#[error("Workspace id can not be empty or whitespace")]
2525
WorkspaceIdInvalid = 101,
2626

27-
#[display(fmt = "Color style of the App is invalid")]
27+
#[error("Color style of the App is invalid")]
2828
AppColorStyleInvalid = 102,
2929

30-
#[display(fmt = "Workspace desc is invalid")]
30+
#[error("Workspace desc is invalid")]
3131
WorkspaceDescTooLong = 103,
3232

33-
#[display(fmt = "Workspace description too long")]
33+
#[error("Workspace description too long")]
3434
WorkspaceNameTooLong = 104,
3535

36-
#[display(fmt = "App id can not be empty or whitespace")]
36+
#[error("App id can not be empty or whitespace")]
3737
AppIdInvalid = 110,
3838

39-
#[display(fmt = "App name can not be empty or whitespace")]
39+
#[error("App name can not be empty or whitespace")]
4040
AppNameInvalid = 111,
4141

42-
#[display(fmt = "View name can not be empty or whitespace")]
42+
#[error("View name can not be empty or whitespace")]
4343
ViewNameInvalid = 120,
4444

45-
#[display(fmt = "Thumbnail of the view is invalid")]
45+
#[error("Thumbnail of the view is invalid")]
4646
ViewThumbnailInvalid = 121,
4747

48-
#[display(fmt = "View id can not be empty or whitespace")]
48+
#[error("View id can not be empty or whitespace")]
4949
ViewIdInvalid = 122,
5050

51-
#[display(fmt = "View desc too long")]
51+
#[error("View desc too long")]
5252
ViewDescTooLong = 123,
5353

54-
#[display(fmt = "View data is invalid")]
54+
#[error("View data is invalid")]
5555
ViewDataInvalid = 124,
5656

57-
#[display(fmt = "View name too long")]
57+
#[error("View name too long")]
5858
ViewNameTooLong = 125,
5959

60-
#[display(fmt = "Connection error")]
60+
#[error("Connection error")]
6161
ConnectError = 200,
6262

63-
#[display(fmt = "Email can not be empty or whitespace")]
63+
#[error("Email can not be empty or whitespace")]
6464
EmailIsEmpty = 300,
65-
#[display(fmt = "Email format is not valid")]
65+
#[error("Email format is not valid")]
6666
EmailFormatInvalid = 301,
67-
#[display(fmt = "Email already exists")]
67+
#[error("Email already exists")]
6868
EmailAlreadyExists = 302,
69-
#[display(fmt = "Password can not be empty or whitespace")]
69+
#[error("Password can not be empty or whitespace")]
7070
PasswordIsEmpty = 303,
71-
#[display(fmt = "Password format too long")]
71+
#[error("Password format too long")]
7272
PasswordTooLong = 304,
73-
#[display(fmt = "Password contains forbidden characters.")]
73+
#[error("Password contains forbidden characters.")]
7474
PasswordContainsForbidCharacters = 305,
75-
#[display(fmt = "Password should contain a minimum of 6 characters with 1 special 1 letter and 1 numeric")]
75+
#[error("Password should contain a minimum of 6 characters with 1 special 1 letter and 1 numeric")]
7676
PasswordFormatInvalid = 306,
77-
#[display(fmt = "Password not match")]
77+
#[error("Password not match")]
7878
PasswordNotMatch = 307,
79-
#[display(fmt = "User name is too long")]
79+
#[error("User name is too long")]
8080
UserNameTooLong = 308,
81-
#[display(fmt = "User name contain forbidden characters")]
81+
#[error("User name contain forbidden characters")]
8282
UserNameContainForbiddenCharacters = 309,
83-
#[display(fmt = "User name can not be empty or whitespace")]
83+
#[error("User name can not be empty or whitespace")]
8484
UserNameIsEmpty = 310,
85-
#[display(fmt = "user id is empty or whitespace")]
85+
#[error("user id is empty or whitespace")]
8686
UserIdInvalid = 311,
87-
#[display(fmt = "User not exist")]
87+
#[error("User not exist")]
8888
UserNotExist = 312,
89-
#[display(fmt = "Text is too long")]
89+
#[error("Text is too long")]
9090
TextTooLong = 400,
9191

92-
#[display(fmt = "Grid id is empty")]
92+
#[error("Grid id is empty")]
9393
GridIdIsEmpty = 410,
94-
#[display(fmt = "Grid view id is empty")]
94+
#[error("Grid view id is empty")]
9595
GridViewIdIsEmpty = 411,
9696

97-
#[display(fmt = "Grid block id is empty")]
97+
#[error("Grid block id is empty")]
9898
BlockIdIsEmpty = 420,
99-
#[display(fmt = "Row id is empty")]
99+
#[error("Row id is empty")]
100100
RowIdIsEmpty = 430,
101-
#[display(fmt = "Select option id is empty")]
101+
#[error("Select option id is empty")]
102102
OptionIdIsEmpty = 431,
103-
#[display(fmt = "Field id is empty")]
103+
#[error("Field id is empty")]
104104
FieldIdIsEmpty = 440,
105-
#[display(fmt = "Field doesn't exist")]
105+
#[error("Field doesn't exist")]
106106
FieldDoesNotExist = 441,
107-
#[display(fmt = "The name of the option should not be empty")]
107+
#[error("The name of the option should not be empty")]
108108
SelectOptionNameIsEmpty = 442,
109-
#[display(fmt = "Field not exists")]
109+
#[error("Field not exists")]
110110
FieldNotExists = 443,
111-
#[display(fmt = "The operation in this field is invalid")]
111+
#[error("The operation in this field is invalid")]
112112
FieldInvalidOperation = 444,
113-
#[display(fmt = "Filter id is empty")]
113+
#[error("Filter id is empty")]
114114
FilterIdIsEmpty = 445,
115-
#[display(fmt = "Field is not exist")]
115+
#[error("Field is not exist")]
116116
FieldRecordNotFound = 446,
117117

118-
#[display(fmt = "Field's type-option data should not be empty")]
118+
#[error("Field's type-option data should not be empty")]
119119
TypeOptionDataIsEmpty = 450,
120120

121-
#[display(fmt = "Group id is empty")]
121+
#[error("Group id is empty")]
122122
GroupIdIsEmpty = 460,
123123

124-
#[display(fmt = "Invalid date time format")]
124+
#[error("Invalid date time format")]
125125
InvalidDateTimeFormat = 500,
126126

127-
#[display(fmt = "The input string is empty or contains invalid characters")]
127+
#[error("The input string is empty or contains invalid characters")]
128128
UnexpectedEmptyString = 999,
129129

130-
#[display(fmt = "Invalid data")]
130+
#[error("Invalid data")]
131131
InvalidData = 1000,
132132

133-
#[display(fmt = "Serde")]
133+
#[error("Serde")]
134134
Serde = 1001,
135135

136-
#[display(fmt = "Protobuf serde")]
136+
#[error("Protobuf serde")]
137137
ProtobufSerde = 1002,
138138

139-
#[display(fmt = "Out of bounds")]
139+
#[error("Out of bounds")]
140140
OutOfBounds = 10001,
141141
}
142142

0 commit comments

Comments
 (0)