116 lines
2.9 KiB
Rust
Raw Normal View History

2021-07-13 17:19:39 +08:00
use derive_more::Display;
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use flowy_dispatch::prelude::{EventResponse, ResponseBuilder};
use std::convert::TryInto;
#[derive(Debug, Default, Clone, ProtoBuf)]
pub struct WorkspaceError {
#[pb(index = 1)]
pub code: WorkspaceErrorCode,
#[pb(index = 2)]
pub msg: String,
}
impl WorkspaceError {
2021-07-13 23:08:20 +08:00
pub fn new(code: WorkspaceErrorCode, msg: &str) -> Self {
2021-07-13 17:19:39 +08:00
Self {
code,
msg: msg.to_owned(),
}
}
}
#[derive(Debug, Clone, ProtoBuf_Enum, Display, PartialEq, Eq)]
pub enum WorkspaceErrorCode {
#[display(fmt = "Unknown")]
Unknown = 0,
#[display(fmt = "Workspace name is invalid")]
WorkspaceNameInvalid = 1,
#[display(fmt = "Workspace id is invalid")]
WorkspaceIdInvalid = 2,
2021-07-23 22:42:44 +08:00
#[display(fmt = "Color style of the App is invalid")]
2021-07-13 17:19:39 +08:00
AppColorStyleInvalid = 3,
2021-07-23 22:42:44 +08:00
#[display(fmt = "Id of the App is invalid")]
2021-07-19 22:44:37 +08:00
AppIdInvalid = 10,
2021-07-23 22:42:44 +08:00
#[display(fmt = "Name of the App is invalid")]
2021-07-19 22:44:37 +08:00
AppNameInvalid = 11,
2021-07-23 22:42:44 +08:00
#[display(fmt = "Name of the View is invalid")]
2021-07-19 22:44:37 +08:00
ViewNameInvalid = 20,
#[display(fmt = "Thumbnail of the view is invalid")]
2021-07-23 22:42:44 +08:00
ViewThumbnailInvalid = 21,
#[display(fmt = "Id of the View is invalid")]
ViewIdInvalid = 22,
#[display(fmt = "Description of the View is invalid")]
ViewDescInvalid = 23,
2021-07-13 23:08:20 +08:00
#[display(fmt = "Get database connection failed")]
2021-07-19 22:44:37 +08:00
DatabaseConnectionFail = 100,
2021-07-13 23:08:20 +08:00
2021-07-13 17:19:39 +08:00
#[display(fmt = "Database internal error")]
2021-07-19 22:44:37 +08:00
WorkspaceDatabaseError = 101,
#[display(fmt = "User internal error")]
2021-07-19 22:44:37 +08:00
UserInternalError = 102,
2021-07-16 23:18:12 +08:00
#[display(fmt = "User not login yet")]
2021-07-19 22:44:37 +08:00
UserNotLoginYet = 103,
2021-07-13 17:19:39 +08:00
}
impl std::default::Default for WorkspaceErrorCode {
fn default() -> Self { WorkspaceErrorCode::Unknown }
}
impl std::convert::From<flowy_database::result::Error> for WorkspaceError {
fn from(error: flowy_database::result::Error) -> Self {
ErrorBuilder::new(WorkspaceErrorCode::WorkspaceDatabaseError)
2021-07-13 17:19:39 +08:00
.error(error)
.build()
}
}
impl flowy_dispatch::Error for WorkspaceError {
fn as_response(&self) -> EventResponse {
let bytes: Vec<u8> = self.clone().try_into().unwrap();
ResponseBuilder::Err().data(bytes).build()
}
}
pub struct ErrorBuilder {
pub code: WorkspaceErrorCode,
pub msg: Option<String>,
}
impl ErrorBuilder {
pub fn new(code: WorkspaceErrorCode) -> Self { ErrorBuilder { code, msg: None } }
pub fn msg<T>(mut self, msg: T) -> Self
where
T: Into<String>,
{
self.msg = Some(msg.into());
self
}
pub fn error<T>(mut self, msg: T) -> Self
where
T: std::fmt::Debug,
{
self.msg = Some(format!("{:?}", msg));
self
}
pub fn build(mut self) -> WorkspaceError {
WorkspaceError::new(self.code, &self.msg.take().unwrap_or("".to_owned()))
}
}